簡體   English   中英

無法解析cURL請求PHP上的主機

[英]Could not resolve host on cURL request PHP

在本地主機(Mac OS X El Capitan)上成功測試之后,我開始構建我的第一個API,然后將其上傳到運行Ubuntu 16.04的digitalocean服務器上。

我都嘗試過從本地主機和存儲API的服務器運行cURL請求,並且它們都返回錯誤號為6的“無法解析主機”錯誤。我的代碼:

在API方面

echo "200";exit;

在客戶端

$token = 'this is the session token';
$url = "http://xx.xx.xx.xx/API/index.php";
$data = array('username'=>'username','password'=>'password');
$datajson = json_encode($data);

$message = $url . $datajson . 'PUT';
$publishThis = base64_encode(
        hash_hmac('sha256', $message, 'secret key', true)
    );
    $len = 'Content-Length: ' . strlen($message);
$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, urlencode($url));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',$len, 'Authorization: ' . $publishThis));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch, CURLOPT_POSTFIELDS,$datajson);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    $response  = curl_exec($ch);

請幫助!!

PS:如果我刪除urlencode函數,請求將花費大約20分鍾的時間,並以400錯誤的請求錯誤結束

之后編輯:

$token = 'this is the session token';
$url = "http://xx.xx.xx.xx/API/index.php";
$data = array('username'=>'username','password'=>'password');
$datajson = json_encode($data);
$build_query = http_build_query($data);

$message = $url . $datajson . 'PUT';
$publishThis = base64_encode(
        hash_hmac('sha256', $message, 'secret key', true)
);
$len = 'Content-Length: ' . strlen($build_query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',$len, 'Authorization: ' . $publishThis));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$build_query);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$response  = curl_exec($ch);

if($errno = curl_errno($ch)) {
    $error_message = curl_strerror($errno);
    echo "cURL error ({$errno}):\n {$error_message}";
}   

echo $response;

變化:

$build_query = http_build_query($data);

$len = 'Content-Length: ' . strlen($build_query);

curl_setopt($ch, CURLOPT_POSTFIELDS,$build_query)

CURLOPT_URL刪除URL編碼

暫無
暫無

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

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