【发布时间】:2017-03-30 15:51:57
【问题描述】:
我在邮递员中有一个 API。我想创建一个 CURL 请求并得到适当的响应。这是我的 POSTMAN API。
我已成功收到此回复。
"{\"Request\":{\"state\":\"Manama\",\"address\":\"406 Falcon Tower\",\"address2\":\"Diplomatic Area\",\"city\":\"Manama\",\"country\":\"BH\",\"fullname\":\"Dawar Khan\",\"postal\":\"317\"},\"Response\":{\"status\":\"Success\",\"code\":100,\"message\":\"Address is verified\"}}"
现在我想在我的 PHP 代码中使用这个 API 调用。我使用了这个代码。
$data = array(
'Request' => 'ValidateAddress',
'address' => test_input($form_data->address),
'secondAddress' => test_input($form_data->secondAddress),
'city' => test_input($form_data->city),
'country' => test_input($form_data->country),
'name' => test_input($form_data->name),
'zipCode' => test_input($form_data->zipCode),
'merchant_id' => 'shipm8',
'hash' => '09335f393d4155d9334ed61385712999'
);
$data_string = json_encode($data);
$url = 'myurl.com/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
curl_close($ch);
$json_result = json_decode($result, true);
echo '<pre>';print_r($json_result);echo '</pre>';
但我看不到我的 $json_result。它只是在视图中回显<pre></pre>。谁能指导我?提前致谢。我想得到我的Response。
更新
我使用 curl_error,它给了我以下错误。
Curl 错误:SSL 证书问题:证书链中的自签名证书
【问题讨论】:
-
使用
curl_error检查Curl是否有任何错误。您可以参考this answer查找错误。 -
postman生成的代码你试过了吗?
-
Curl 错误:SSL 证书问题:证书链中的自签名证书@BhavikShah
-
你应该检查
this answer。它与证书有关。 -
@BhavikShah 我已经检查过它是否有效。谢谢。