这是我用来解决 curl 连接问题的代码:
$url = 'https://myurl.com';
$encodedurl = urlencode($url);
$token = 'my_api_key';
$postfields = 'my data';
$_h = curl_init($newurl);
curl_setopt ($_h, CURLOPT_HTTPHEADER, array('Authentication: Token=mytoken'));
curl_setopt($_h, CURLOPT_HEADER, 1);
curl_setopt($_h, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($_h, CURLOPT_POST, 1);
curl_setopt($_h, CURLOPT_URL, encodedurl);
curl_setopt($_h, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
curl_setopt($_h, CURLOPT_DNS_CACHE_TIMEOUT, 2 );
curl_setopt ($_h, CURLOPT_POSTFIELDS, $postfields);
var_dump(curl_exec($_h));
var_dump(curl_getinfo($_h));
var_dump(curl_error($_h));
当我执行此操作时,我收到此作为数组的一部分:
["url"]=> string(59) "HTTP://https://myencodedurl.com/"
有人知道如何摆脱它添加到开头的 HTTP://吗?我已经在每个可能的地方尝试了 str_replace,但它似乎只在curl_exec 触发时才会发生。
这导致我的应用程序失败,因为无法访问资源...
最佳答案
Urlencoded URL 不再是 URL。它错过了协议(protocol)部分,因为 ://
进行了百分比编码(例如 https%3A%2F%2Fmyurl.com
),因此curl 假设并添加 http://
。但 http://https%3A%2F%2Fmyurl.com
仍然不是有效的 URL,请求失败。
关于PHP - Curl 正在添加 HTTP ://to my HTTPS URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31844865/