使用cURL執行Post網路請求

Table of Contents

Table of Contents

使用PHP的cURL做POST網路請求。

使用範例

private function requestByCurl($input) {
        try {
            $apiUrl = "{https://your_request_url}";
            $postData = array(
                "category" => 1,
                "store_name" => "テスト_",
                "store_id" => 123
                "timestamp" => time(),
            );
            $header = array(
                'Content-Type: application/json',
                'Authorization: JWT '.self::API_KEY
            );
            $ch = curl_init($apiUrl);
            curl_setopt($ch, CURLOPT_POST, true); // POST
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData)); 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            curl_setopt($ch, CURLOPT_HEADER, true);
            $response = curl_exec($ch);
            $info    = curl_getinfo($ch);
            $errorNo = curl_errno($ch);

            log_message("debug", "post data:".print_r(json_encode($postData), true));
            log_message("debug", "info:".print_r($info, true));
            log_message("debug", "response:".print_r($response, true));
            curl_close($ch);
        } catch (Exception $e) {
            log_message("error", $e->getMessage());
        }
    }