使用基本http身份验证无法正常工作
我正在尝试使用curl连接到WS。 我的代码:使用基本http身份验证无法正常工作
$url ="https://example?param=ee¶m2=rr";
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode("username:password") // <---
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_HEADER,true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($curl, CURLOPT_USERPWD, 'username:passwrd');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
我得到这个错误:
'content_type' => NULL, 'http_code' => 0, 'header_size' => 0, 'request_size' => 0, 'filetime' => -1, 'ssl_verify_result' => 0, 'redirect_count' => 0, 'total_time' => 0.051353999999999997, 'namelookup_time' => 0.0032659999999999998, 'connect_time' => 0.051365000000000001, 'pretransfer_time' => 0, 'size_upload' => 0, 'size_download' => 0, 'speed_download' => 0, 'speed_upload' => 0, 'download_content_length' => -1, 'upload_content_length' => -1, 'starttransfer_time' => 0, 'redirect_time' => 0, 'certinfo' => array (), 'primary_ip' => '185.18.224.30', 'primary_port' => 443, 'local_ip' => '172.23.155.94', 'local_port' => 44608, 'redirect_url' => '',)error occured during curl exec.
在我的代码我无法找出错误。错过了一个参数?
尝试添加:
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
默认情况下,当您使用HTTPS服务器上的协议,袅袅检查SSL sertificate,如果它是无效的给出了一个错误。上面的这一行关闭了这一点。
虽然这可能是正确的,但值得注意的是,关闭cURL验证是一种非常糟糕的做法,因为如果TLS安全网站无法通过身份验证,它可能无法得到保护。 (然而,修复此验证可能是另一个问题)。 [参考](https://curl.haxx.se/docs/sslcerts.html) – Martin
$url ="https://example?";
$headers = array(
'Content-Type:application/json',
'Authorization: Basic '. base64_encode("username:password")
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,"param=ee¶m2=rr");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
// execute the request
$output = curl_exec($ch);
你可以编辑你的答案并解释你改变了什么以及你的代码为什么工作? – Martin
thx但我是我使用get方法,所以我不需要'curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,“POST”);'' –
作者:WS你是指网站,而不是.ws网站? – Martin
您没有使用CURLOPT_USERPWD的原因,而不是自己组装授权头? – CBroe
它在标题数组上设置 –