无法根据 cURL 请求解析主机 PHP
Could not resolve host on cURL request PHP
我开始构建我的第一个 API,在本地主机上成功测试后(Mac OS X El Capitan)我将它上传到我的 digitalocean 服务器 运行宁 Ubuntu 16.04.
我都尝试过 运行 来自本地主机和存储 API 的服务器的 cURL 请求,但它们都 return "Could not resolve host" 错误错误编号 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 错误请求错误结束
后期编辑:
$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 编码
我开始构建我的第一个 API,在本地主机上成功测试后(Mac OS X El Capitan)我将它上传到我的 digitalocean 服务器 运行宁 Ubuntu 16.04.
我都尝试过 运行 来自本地主机和存储 API 的服务器的 cURL 请求,但它们都 return "Could not resolve host" 错误错误编号 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 错误请求错误结束
后期编辑:
$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