我有几个正在通过php curl运行的URL,以便我可以获取HTTP代码。有趣的是,一些URL的工作正常,并返回200,但其他人总是返回400.只有当我从数据库中提供URL时,如果我通过硬编码运行它,然后返回200,它应该。PHP cURL在服务器上返回400
我试过urlencoding /解码,它不是问题。外观看起来很好。我能够发现的一个奇怪的事情是,400 URL始终有一个[content_type] => text/html; charset=iso-8859-1
。网址也很简单https://sometext.something.something/test
,没有任何参数
这是我一直在运行URL的PHP代码。它适用于我的本地主机,但在服务器上会导致问题。
function checkStatus($url) {
$urlencode=urlencode($url);
foreach (explode('&', $urlencode) as $chunk) {
$param = explode("=", $chunk);
if ($param) {
echo urldecode($param[0]);
$ch = curl_init(urldecode($param[0]));
}
}
//$ch = curl_init($urlencode);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// curl_setopt($ch, CURLINFO_CONTENT_TYPE, true);
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:text/html;charset=UTF-8'));
curl_exec($ch);
$returnCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error_report= curl_getinfo($ch);
print_r($error_report);
curl_close($ch);
return $returnCode;
}
这里的错误消息的示例:
Array
(
[url] => https://url.net/something
[content_type] => text/html; charset=iso-8859-1
[http_code] => 400
[header_size] => 171
[request_size] => 83
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.056926
[namelookup_time] => 0.003457
[connect_time] => 0.004276
[pretransfer_time] => 0.01614
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => 0
[starttransfer_time] => 0.056906
[redirect_time] => 0
[certinfo] => Array
(
)
)
您是否尝试过使用parse_url而不是urlencode/urldecode? –
@ Arkadiusz'flies'Rzadkowolski不,我没有。现在测试。编辑:嗯,我真的不知道它在这里如何帮助。它以预期的格式返回url,但没有其他的东西。 – Envious