我必须写一个API,似乎没有任何作用。当我使用apitester.com时,它可以工作。当我使用我的应用程序时,它不会。我输出的标头和有效负载在两者之间看起来是一样的,所以我假设我做错了什么。这是我的PHP,用于将数据发送到API
<?php
$email = $_POST['email'];
$expired = NULL;
$funded = TRUE;
$data = array(
"email" => $email,
"expired" => $expired,
"funded" => $funded
);
$url = 'https://my.rest.api';
$json_string = json_encode($data);
$headers = array (
"Content-Type: application/json",
"Authorization: Bearer xxx"
);
$channel = curl_init($url);
curl_setopt($channel, CURLOPT_RETURNTRANSFER, true);
curl_setopt($channel, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($channel, CURLOPT_HTTPHEADER, $headers);
curl_setopt($channel, CURLOPT_POSTFIELDS, $json_string);
curl_setopt($channel, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($channel, CURLOPT_CONNECTTIMEOUT, 10);
$statusCode = curl_getInfo($channel, CURLINFO_HTTP_CODE);
curl_exec($channel);
http_response_code($statusCode);
if ( $statusCode != 200 ){
echo "Data submitted was ".$json_string." Returned status code: {$statusCode} \n".curl_error($channel);
} else {
echo $response;
}
//I turn the below 2 lines on and off to see what I am actually sending
// print_r($headers);
// echo $json_string;
curl_close($channel);
?>
我在应用程序上得到的返回状态码为“ 0”,但使用测试仪返回的状态码为“ 200”。我发送的curl选项是否明显有问题?
答案 0 :(得分:1)
如果状态为0,则表示HTTP请求根本没有完成。您可以使用以下功能来找出发生了什么错误:
侧面注释:
$response
,但是该变量不存在。CURLOPT_SSL_VERIFYPEER
是一个非常糟糕的主意。在生产之前,请确保将其删除。