cuRL returnin 404 error

后端 未结 3 1534
猫巷女王i
猫巷女王i 2020-12-06 11:23

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         


        
3条回答
  •  执念已碎
    2020-12-06 12:09

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

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

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

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

    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) 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) 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 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);
    

    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) Also, you can set the referer by

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

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

提交回复
热议问题