3 Replies - 4866 Views - Last Post: 19 November 2014 - 12:42 AM

#1 ethereal1m   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 227
  • Joined: 30-June 09

Got 302 error when requesting URL vie PHP Curl

Posted 18 November 2014 - 03:22 AM

Dear all,
I got 302 error message when requesting a url via PHP curl_exec, but got code 200 when do it manually. What could possibly be the difference that makes the result different?

Suppose I have the following code in the server:

$url = "http://localhost/circle/my_request/susan?confirm_key=c429d674e36ffe1a4f87fd5a17a0200dcfff0884e8bb3801d2a7d559dcc8d8cd1416295160";
$ch = @curl_init($url);
@curl_setopt($ch, CURLOPT_HEADER, true);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
@curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Circle)");
@curl_setopt($ch, CURLOPT_TIMEOUT,60);
$s = @curl_exec($ch);
$curl_info = @curl_getinfo($ch);
$http_code = $curl_info['http_code'];



In this case, $http_code == 302, or the requested URL is redirected. But when I put the URL manually to the browser, it didn't do the redirection, and took me to the correct place.

Do I miss anything? How come those 2 requests result differently?

Much appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Got 302 error when requesting URL vie PHP Curl

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3134
  • View blog
  • Posts: 8,931
  • Joined: 15-January 14

Re: Got 302 error when requesting URL vie PHP Curl

Posted 18 November 2014 - 10:35 AM

How do you know it's not doing a redirection when you enter it manually, are you using your browser's developer tools to check the response and make sure it's not redirecting?

Quote

What could possibly be the difference that makes the result different?

The server could be checking the headers and redirecting based on those.
Was This Post Helpful? 1
  • +
  • -

#3 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3215
  • View blog
  • Posts: 12,098
  • Joined: 03-December 12

Re: Got 302 error when requesting URL vie PHP Curl

Posted 18 November 2014 - 04:02 PM

These are methods pulled out of a class used to check site status. You may find them useful.


   /**
     *
     * @param string $url
     * @return int returns the response code from a url passed to the function
     *                 to determine if the URL is currently functioning
     */
    function get_http_response_code($url)
    {
        $headers = get_headers($url);
        return substr($headers[0], 9, 3);
    }
    /**
     * 
     * @param string $url 
     * @return bool
     * checks if passed url returns active header code
     */
    public function checkActiveSite( $url )
    {

        $isActive = $this->get_http_response_code($url) == 200 ? true : false;

        return $isActive;
    }

This post has been edited by astonecipher: 18 November 2014 - 04:03 PM

Was This Post Helpful? 1
  • +
  • -

#4 ethereal1m   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 227
  • Joined: 30-June 09

Re: Got 302 error when requesting URL vie PHP Curl

Posted 19 November 2014 - 12:42 AM

@as and @astonecipher,
I used xdebug to step through when I enter the manual URL and it didn't redirect. Then I do this to trace the redirection:
 @curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);



And look at the output execution that shows the redirection. It turned out that I got a bug in the code server that did redirection.

Thanks for the help guys
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1