无法在cURL请求PHP上解析主机


Could not resolve host on cURL request PHP

我开始构建我的第一个API,在本地主机(Mac OS X El Capitan)上成功测试后,我将其上传到运行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);

请帮助! !

p。S:如果我删除urlencode函数,请求需要大约20分钟,并以400坏请求错误结束

LATER EDIT:

$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编码