如何正确发送POSTFIELDS到cURL

时间:2019-05-28 11:38:53

标签: php curl

我需要通过cURL发送POST数据,如图所示。

image with POST data

我有此代码

 $data = [
            'action' => 'order_cost',
            'address' => 'http://91.211.117.3:720'
        ];

  $query = http_build_query($data);

   $url = "https://ap4.taxi/api/TaxiAPI.php";
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36');
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvJFySHvqeKppEN9W',
            )
        );
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
        $output = curl_exec($ch);

        curl_close($ch);

        var_dump($output);

但是我得到一个错误

image with error

我已经尝试了很多选择。邮递员正常发送POST,我收到答复。

请告诉我,我什至无法想象该怎么做。

2 个答案:

答案 0 :(得分:0)

正如我从您的代码中看到的那样,您仅通过POST方法发送两个字段(操作和地址) 请给我们显示代码https://ap4.taxi/api/TaxiAPI.php,以便您处理接收到的数据。

答案 1 :(得分:0)

function execute_curl($url, $curlopt = array()){
    $ch = curl_init();
    $strCookie = session_name().'='.session_id().'; path=/';
    session_write_close();
    $default_curlopt = array(
        CURLOPT_URL => $url,
        CURLOPT_HEADER => 0,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_COOKIE => $strCookie,
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 AlexaToolbar/alxf-1.54 Firefox/3.6.13 GTB7.1"
    );
    $curlopt = $curlopt + $default_curlopt;

    curl_setopt_array($ch, $curlopt);
    $response = curl_exec($ch);
    $errormsg = curl_error($ch);
        $errorCode = curl_errno($ch);
    $results = array();
    if($errormsg)
    {
        $results['status'] = 'error';
        $results['data'] = $errormsg;
        $results['errorcodetxt'] = curl_error_codes($errorCode);
    }
    else
    {
        $results['status'] = 'success';
        $results['data'] = $response;
    }
    curl_close($ch);
    return $results;
}

$curlopt = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => 1);
$curlresponse = execute_curl($url, $curlopt);