cuRL 返回 404 错误 [英] cuRL returnin 404 error

查看:169
本文介绍了cuRL 返回 404 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 cuRL 从远程服务器获取一些数据...响应采用 JSON 格式..这是我的代码:

I`m using cuRL to get some data from remote server... The response is in JSON format.. This is my code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_URL, 'http://www.myaddress.com/mypage.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, array("id" => $id));
$return = curl_exec($ch);
curl_close($ch);

如果我在浏览器中访问链接,页面加载正常,但如果我通过 cuRL 访问返回 404 错误...

If I access the link in the browser the page load OK, but if I access through the cuRL return a 404 error...

推荐答案

我可以猜出一些可以从服务器端检查的东西,以显示错误.

I can guess a few things that it can be checked from the server side, to show the error.

1) 正如其他答案中所述,请务必设置所有必要的标题,您可以检查它们,例如由萤火虫,如这里所示,

1) As it is stated in other answers, be sure to set all the necessary headers, you can check them e.g. by firebug, as it is shown in here,

或者您可以通过 php get_headers 获取标题功能.设置它使用

or you can get the headers by php get_headers function. to set it use

curl_setopt($ch, CURLOPT_HTTPHEADER, array("HeaderName: HeaderValue"));

2) 当您在浏览器中打开一个页面时(不包括使用 post 方法提交表单)它会发出一个 get 请求,而不是 post,所以如果在服务器端它被检查 $_GET,那么您的帖子请求将不会被考虑.

2) When you open a page in the browser(excluding form submit with post method) it makes a get request, instead of post, so if in the server side it is checked $_GET, then your post request will not be considered.

3) 如果你确定它应该是一个 post 请求(比如说,它是一个表单提交),那么以下可能是一个问题:一些表单可能有隐藏字段,那又是在服务器中被检查,如果没有设置,可能会返回错误.因此,您应该查看表单的源代码并将它们(如果有)添加到您的帖子参数中.

3) If you sure that it should be a post request(say, it is a form submit), then the following can be a problem: some forms can have hidden fields, that again are being checked in the server, and if they are not set, error can be returned. So, you should look at the source code of the form and add them(if there are any) to your post parameters.

4) 如果您要提交表单,请确保也使用其名称和值设置提交按钮,因为与隐藏字段类似,也可以选中此项.

4) if you are submitting a form, be sure to set the submit button with its name and value as well, because similar to hidden fields, this can be checked as well.

5) Cookies 也可能是一个问题,因为默认浏览器有它,而 curl 没有.要能够设置和读取 cookie 使用此代码

5) Cookies can be a problem as well, because by default browser has it , and curl does not. To to able to set and read cookies use this code

// set cookie 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    
// use cookie
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);

在这里,$cookie_file cookie 文件的路径.linux或mac下不知道,windows下一定要使用绝对路径的cookie文件.

here, $cookie_file path to the cookies file. Do not know in linux or mac, but in windows be sure to use absolute path to the cookie file.

6) 另外,您可以通过

curl_setopt($ch, CURLOPT_REFERER, 'http://www.myaddress.com/mypage.php');

在ajax请求的情况下,您可能想要添加一个标头X-Requested-With,其值为XMLHttpRequest

In case of ajax request you might want to add a header X-Requested-With with value as XMLHttpRequest

这篇关于cuRL 返回 404 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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