在PHP中使用https curl_exec调用发送和接收JSON [英] Send and Receive JSON using an https curl_exec call in PHP

查看:252
本文介绍了在PHP中使用https curl_exec调用发送和接收JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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);


解决方案



问题是主机的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);


I'm trying to make a curl call using PHP. My project has a flow like this

  • I redirect user to the server.
  • User uses the services and the server redirects him to a page on my application.
  • I would like to make a server to server call at this point to verify certain details.

Point to be noted - I post JSON data as well as receive JSON data as response.

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);

解决方案

As you mention in the comment:

The problem is with SSL verification of the host. 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:

curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);

这篇关于在PHP中使用https curl_exec调用发送和接收JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆