$url = 'abc,com/def.php';
$ch = curl_init();
$cert = "xyz.pem";
if (!stat($cert))
{
die('No cert');
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLCERT, $cert);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'hidden');
curl_setopt($ch, CURLOPT_SSLVERSION, 5);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
if($result = curl_exec($ch) === false)
{
die('Curl error: ' . curl_error($ch));
}
curl_close($ch);
但是我们需要能够读取我们正在返回的json成功消息(响应)(参见下面的示例)到php脚本中以进一步编写脚本。我们知道我们可以使用解码来做到这一点。所以这不是问题。请继续阅读....
以下是我们从Rightmove回来的json字符串成功消息的示例 - 完全符合预期。
{
"message": "Your property has successfully been uploaded to Rightmove",
"success": true,
"errors": null,
"warnings": [],
"request_timestamp": "02-08-2016 13:07:50",
"response_timestamp": "02-08-2016 13:07:50",
"request_id": "231301-160802-130750-10990",
"property": {
"agent_ref": "agent950",
"rightmove_id": 123456789,
"change_type": "UPDATE",
"rightmove_url": "http://www.adftest.rightmove.com/property-to-rent/property-123456789.html"
}
}
我们现在需要测试的是如何将上面的json响应数据合并到我们的PHP脚本中,从下面的json解码片段开始......
$var = json_decode($xxx, true);//outputs and associative array
print_r($var);//prints the array
一旦我们将$ var对象恢复,我们就可以在脚本中使用其中的数据。
问题 .....我们需要知道的是$ xxx(即json字符串成功消息响应的名称是什么,所以我们可以将它添加到上面的解码片段并继续使用。或者我们如何设置$ xxx.Rightmove无能为力。
我怀疑我们可能会错过这里显而易见的事情,当你告诉我答案时我会畏缩。
感谢您的期待
答案 0 :(得分:0)
正如我在回答之前的评论中所说,这应该很容易:
$url = 'abc,com/def.php';
$ch = curl_init();
$cert = "xyz.pem";
if (!stat($cert))
{
die('No cert');
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLCERT, $cert);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, 'hidden');
curl_setopt($ch, CURLOPT_SSLVERSION, 5);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
if($result = curl_exec($ch) === false)
{
die('Curl error: ' . curl_error($ch));
}
curl_close($ch);
$var = json_decode($result, true);//outputs and associative array
print_r($var);//prints the array
传入变量$result
。请查看documentation以获取解释:
混合json_decode(字符串$ json [,bool $ assoc = false [,int $ depth = 512 [,int $ options = 0]]])
第一个变量是一个字符串,即JSON字符串。
编辑:经过研究,我发现:
Rightmove提供客户端证书,带密钥库密码的公钥文件和根证书。
所以:
$json = "json string";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '\pemfile.pem');
curl_setopt($ch, CURLOPT_URL, "https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/json'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSH_PRIVATE_KEYFILE, getcwd() . '\JKSFILE.jks');
curl_setopt($ch, CURLOPT_SSLCERT, getcwd() . '\PEMFILE.pem');
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "THESSLPASSWORD");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_REFERER, "https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE , 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$ch_result = curl_exec($ch);
print curl_errno($ch);
print curl_error($ch);
echo "Result = ".$ch_result;
curl_close($ch);
可能会做到这一点。