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

查看:307
本文介绍了cuRL returnin 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返回访问a 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)正如其他答案中所述,请务必设置所有必要的标题,例如通过firebug,如这里所示,
http://s30.postimg.org /qjcbmdye9/facebook_headers.png

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, http://s30.postimg.org/qjcbmdye9/facebook_headers.png

,或者您可以通过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) Cookie也可能是一个问题,因为默认情况下浏览器有它,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 文件。不知道在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');

EDIT:如果是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 returnin 404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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