[英]Send and Receive JSON using an https curl_exec call in PHP
I'm trying to make a curl call using PHP. 我正在尝试使用PHP进行curl调用。 My project has a flow like this
我的项目有这样的流程
Point to be noted - I post JSON data as well as receive JSON data as response. 需要注意的一点-我发布了JSON数据并接收了JSON数据作为响应。
I have tried this. 我已经试过了。 I am able to send data but don't get any response.
我能够发送数据,但是没有任何回应。
Is this correct? 这个对吗? Please help!
请帮忙!
$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);
Try to use the below code which works for me. 尝试使用以下对我有用的代码。 and make sure the json_url is correct.
并确保json_url是正确的。 When you run it on the browser you should get an output.
在浏览器上运行它时,您应该得到一个输出。 Before try it using CURL, run in on the browser and see whether you get the output you want.
在使用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);
As you mention in the comment: 正如您在评论中提到的:
The problem is with SSL verification of the host. 问题在于主机的SSL验证。 It takes some effort (see http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/ ) to setup SSL correctly, so a workaround is to add the following two options:
正确设置SSL需要花费一些精力(请参阅http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/ ),因此解决方法是添加以下两个选项:
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.