簡體   English   中英

PHP發布到API(嘗試使用cURL)

[英]PHP post to API (trying with cURL)

如果我直接轉到URL中帶有正確參數的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>";

但是我沒有收到以上代碼的答復。

嘗試這個:

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM