Php-如何在curl中发送帖子请求

时间:2017-08-24 10:16:09

标签: php api curl

我正在研究第三方api并需要使用“xml http headers”在curl中发布数据。我曾尝试过很多方法,但是api没有返回任何错误或任何响应。它返回空响应。这是我的代码

$url = 'https://google.com'; // example url
    $fields=array( 
        'apikey' =>'xxxxxxxxxxxxxxxxxx',
        'totalItemAmount' => '70.00',
        'totalPaymentAmount' =>'70.00',
        'OrderNo' => '1234',
        'PhoneNo' => '1823434423',
        'addressCode' => 'S',
        'lastName' => 'Johnson',
        'firstName' =>'Thomas',
        'addressLine1' => '345 D lake',
        'city' =>'Chicago',
        'stateCode' => 'IL',
        'zipCode' =>'60579',
        'countryCode' => 'US',
        'itemQty' =>'2'
);
    $fields_string = '';
    //url-ify the data for the POST
    foreach($fields as $key=>$value) 
    { 
        $fields_string .= $key.'='.$value.'&'; 
    }
    rtrim($fields_string, '&');
    //open connection

    $ch = curl_init();
    curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER  => array('Content-Type: application/xml', 'Accept: application/xml'),
    CURLOPT_RETURNTRANSFER  =>true,
    CURLOPT_SSL_VERIFYPEER  => false,
    CURLOPT_URL => $url,
    CURLOPT_POST => count($fields),
    CURLOPT_POSTFIELDS => http_build_query($fields) 
));
    //execute post
    $result = curl_exec($ch);
    print_r($result);
    //close connection 
    curl_close($ch);

0 个答案:

没有答案