数据值未发送到api。 PHP + cURL

时间:2017-02-21 18:23:40

标签: php curl

$api = "xyz";
$data = array('emailId'=>'xyz@hotmail.com','name'=>'rohit','address' => 'noida',
        'city' => 'noida',
        'country' => 'india');
$data_json = json_encode($data);

$endpoint = "http://sdasfsafa/100/uszzzer/register";

$headers = array('api-key: '.$api , 'Content-Type: application/json', 'Content-Length: ' . strlen($data_json));
$options = array( 
    CURLOPT_URL => $endpoint,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POST => POST,
    CURLOPT_POSTFIELDS => $data_json,
    CURLOPT_RETURNTRANSFER => true
);
//print_r($headers);
$ch = curl_init();
curl_setopt_array($ch,$options); 
echo $response  = curl_exec($ch);


$decoderesponse = json_decode($response, true);


?>
<pre>
  <?php print_r($decoderesponse); ?>
</pre>

请纠正我错在哪里,感谢任何帮助。我目前正在做的是将数据值发送到api但是api返回一个错误,我在body部分发送的值为null。

Array ( [code] => -888 [status] => failed [message] => Kindly provide all the mandatory fields. (emailId, name, address, city, country) )

1 个答案:

答案 0 :(得分:0)

$data = http_build_query($fields);

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result,true);

只需添加您的阅读器并尝试

在其他情况下尝试'postman'https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop

它可以导出Curl代码,您可以实现到您的文件。我认为这是有帮助的。 :)