2014-04-28 23 views
0

如果我直接轉到具有URL中正確參數的URL,我會收到成功響應。將PHP發佈到API(使用cURL嘗試)

的URL看起來像

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>"; 

但是我從來沒有得到與上面的代碼的響應。

+0

您是否收到錯誤消息? –

+0

[PHP CURL&HTTPS]的可能重複(http://stackoverflow.com/questions/4372710/php-curl-https) –

+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

+0

感謝您的幫助 - 即使那裏有curl_error,頁面也會繼續加載。就好像請求從未完成一樣。 –

+0

等待~2分鐘後,我得到未知的SSL協議錯誤連接到api.theService.com:443, –

+1

curl_setopt($ ch,CURLOPT_SSLVERSION,3); 做到了:) –