PHP发布到API(尝试使用cURL)

时间:2014-04-28 23:42:16

标签: php curl

如果我直接使用URL中右侧的params,我会得到成功的回复。

网址类似于

https://api.theService.com/send?client_id=54&token=7545h5h554&format=json

在PHP中,我尝试过像

这样的东西
error_reporting(E_ALL);
ini_set('display_errors', 1);

$client_id = 54;
$token = '7545h5h554';

$ch = curl_init('https://api.theService.com/send?client_id='.$client_id.'&token='.$token.'&format=json');

// Send the request
$response = curl_exec($ch);

// Check for errors
if($response === FALSE){
    die(curl_error($ch));
}

// Decode the response
$responseData = json_decode($response, TRUE);

echo "<pre>";
echo print_r($responseData);
echo "</pre>";

但是我从来没有得到上述代码的答复。

1 个答案:

答案 0 :(得分:0)

试试这个:

$client_id = 54;
$token = '7545h5h554';
$url  = "https://api.theService.com/send?client_id=$client_id&token=$token&format=json";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); //waits 10 seconds for response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch) or die(curl_error($ch));
echo $page;

您还应该尝试检查curl_error()中的错误消息 http://www.php.net/curl_error