When parsing curl, it gives error 307 Temporary Redirect . Who faced how to get around this?

 header("Content-type:text/html;charset=utf-8"); $url = 'http://wowcircle.com'; $ch = curl_init($url); $uagent = "Opera/9.80 (Windows NT 6.1; WOW64) Presto/2.12.388 Version/12.14"; // Params curl_setopt($ch, CURLOPT_USERAGENT, $uagent); curl_setopt($ch, CURLOPT_HEADER, true); // curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Get html page $html = curl_exec($ch); // Close curl_close($ch); // Search fragments $pattern = '~<div id="content-header">(.*?)</div>~is'; preg_match($pattern, $html, $search_res); // Show searh results echo $search_res[0]; // print_r($search_res); // Show html echo "<pre>"; print_r($html); echo "</pre>"; 
  • 2
    This is not a mistake :) - D-side

2 answers 2

Follow the redirect, as well as at 301 and 302.

Please note that there with the redirect of the PMBC cookie is set:

HTTP request headers, setting cookies

If you do not put the cookie, the server again returns the redirect.

Add this:

 curl_setopt($ch, CURLOPT_COOKIE, "PMBC=22aa1474f93a37691419631a84b22e6c"); 

I do not know how long it will work. If it stops, then read the cookie and send it on the next request after the redirect.

  • Thanks, works :) - sholkingx

Sample code in one of the working projects that remembers the cookie on the first request and passes it after. The code is written in the tradition of OOP, so integration is on your shoulders.

  1. set callback function

    curl_setopt ($ ch, CURLOPT_HEADERFUNCTION, [& $ this, 'headerCallback']);

  2. body (I saved PHPSESSID , you need PMBC )

/ ** * Set CURLOPT_HEADERFUNCTION to save cookies * @param resource $ ch * @param string $ header * @return type * /

 protected function headerCallback($ch, $header) { $id = $this->getIdByCh($ch); $mathes = [ ]; if ( preg_match('/^Set-Cookie:\s*([^;]*)/mi', $header, $mathes) == 1 ) { if ( false !== ($str = strpos($mathes[1], 'PHPSESSID')) ) { $session = substr($mathes[1], $str + strlen('PHPSESSID') + strlen('=')); if ( !isset($this->cookies[$id]['session']) ) $this->cookies[$id]['session'] = $session; } curl_setopt($ch, CURLOPT_COOKIE, $mathes[1]); } return strlen($header); // Needed by curl } 
  1. Define the getIdByCh method, if you later need to use CurlMulti, otherwise you can cut it out.

/ ** * Determine the ID of the child in the queue * /

 protected function getIdByCh($ch) { return array_search($ch, $this->chs); }