我需要在php curl发送自定义HEADER, post在XML或json,并在web api返回响应。搜索,我发现这个尝试解决我的问题与not found requests
:
$ch = curl_init();
LINE 112 curl_setopt($ch, CURLOPT_URL, $urlCall->instance_url . $request);
..
是指:
$urlCall = https://ip.andress/v1/requestActivateCode/;
和$request是一个json参数编码(只是测试):
$params = array("RetailTransactionRequest" =>
array(
"code" => "0000002381237220",
"amount" => $amount,
"upc" => $upc,
"transactionID" => "1234",
"dateTime" => $xIncommDateTime,
"retailerName" => $retailerName
)
);
$request = json_encode($params);
好吧…但是结果是:
Notice: Trying to get property of non-object in /home/ubuntu/public_html/ip.adress/public/test.php on line 112
Curl error: Could not resolve host: {"RetailTransactionRequest":{"code":"0000002381237220","amount":"20.00","upc":"799366289999","transactionID":"1234","dateTime":"2015-06-08T09:09
custom HEADER的信息为:bool(false)
我需要在服务器API中张贴数组,并在php文件....中从服务器接收答案为什么不工作?谢谢。
你的代码有两个缺陷:
<<p> 1日/strong>根据你的说法,$urlCall
是string
。因此,您不需要通过使用$urlCall->instance_url
来检索它的值。只使用$urlCall
。
2
你不能把你的JSON
字符串附加到你调用的URL。你需要使用CURLOPT_POSTFIELDS
。
$urlCall = 'https://ip.andress/v1/requestActivateCode/';
$params = array("RetailTransactionRequest" =>
array(
"code" => "0000002381237220",
"amount" => $amount,
"upc" => $upc,
"transactionID" => "1234",
"dateTime" => $xIncommDateTime,
"retailerName" => $retailerName
)
);
$request = json_encode($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlCall); // note the use of $urlCall
curl_setopt($ch, CURLOPT_POSTFIELDS, $request); // this passes the JSON string to the server
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($request))
); // this is to tell the server that the request contains JSON