即使url工作正常,php中的curl返回404响应代码 [英] Curl in php returns 404 response code even if url is working

查看:924
本文介绍了即使url工作正常,php中的curl返回404响应代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CentOs 6.5中使用的是php 5.4

I am using php 5.4 in CentOs 6.5

我正在尝试获取以下网址的响应代码是我的代码段

I am trying to get response code of a url below is my code snippet

$URL="http://www.bertuccis.com/#menu";
try{

    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($c, CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36');
    curl_setopt($c, CURLOPT_URL, $URL);
    $contents = curl_exec($c);
    $httpCode = curl_getinfo($c, CURLINFO_HTTP_CODE);
    curl_close($c);

    echo 'Resp Code ' . $httpCode;
} catch(Exception $ex){
    print_r($ex);
}

它返回404 Http响应代码.如果我在浏览器中尝试相同的URL,它将返回200 OK.

It returns 404 Http response code. If i try same url in browser, it returns 200 Ok.

我尝试在我的centos机器的终端上使用curl -v,但是它可以正常工作

I tried using curl -v on terminal of my centos machine but its working

这是curl和php-curl的版本信息

Here is the version info of curl and php-curl

在我的centos中的卷曲lib是:curl 7.24.0(x86_64-unknown-linux-gnu)libcurl/7.24.0

Curl lib in my centos is : curl 7.24.0 (x86_64-unknown-linux-gnu) libcurl/7.24.0

我的php信息中的php-curl信息:

php-curl info in my php info :

cURL support => enabled
cURL Information => 7.19.7
Age => 3
Features
AsynchDNS => No
Debug => No
GSS-Negotiate => Yes
IDN => Yes
IPv6 => Yes
Largefile => Yes
NTLM => Yes
SPNEGO => No
SSL => Yes
SSPI => No
krb4 => No
libz => Yes
CharConv => No
Protocols => tftp, ftp, telnet, dict, ldap, ldaps, http, file, https, ftps, scp, sftp
Host => x86_64-redhat-linux-gnu
SSL Version => NSS/3.15.3
ZLib Version => 1.2.3
libSSH Version => libssh2/1.4.2

推荐答案

这是curl中的错误,已在7.28.1中修复(根据此页面

This was a bug in curl and fixed on 7.28.1 (according to this page http://curl.haxx.se/changes.html).

注意:现代浏览器不会根据请求发送片段部分.但是,您发送的卷发会有所不同.

Note: Modern browsers doesnt send fragment part at the request. But your curl sending it, this is making difference.

这篇关于即使url工作正常,php中的curl返回404响应代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆