我正在尝试使用PHP进行curl调用。我的项目有这样的流程
需要注意的一点 - 我发布JSON数据以及接收JSON数据作为响应。
我试过这个。我能够发送数据,但没有得到任何回复。
这是对的吗? 请帮忙!
$data = array("One" => "Onedata", "Two" => "Twodata");
$JsonData = json_encode($data);
$ch = curl_init('https://exampleurl');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $JsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($JsonData))
);
$result = curl_exec($ch);
答案 0 :(得分:0)
尝试使用以下适用于我的代码。并确保json_url是正确的。当您在浏览器上运行它时,您应该获得输出。在使用CURL尝试之前,请在浏览器上运行并查看是否获得所需的输出。
$json_url ='https://exampleurl';
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json')
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$results = curl_exec($ch); // Getting jSON result string
$json_decoded = json_decode($results);
答案 1 :(得分:0)
正如你在评论中提到的那样:
问题在于主机的SSL验证。需要一些努力(请参阅http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/)才能正确设置SSL,因此解决方法是添加以下两个选项:
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);