Example of PHP curl Implementation of Capturing 302 Jump Pages

  • 2021-07-07 06:33:46
  • OfStack

PHP's CURL normal page grabbing program is as follows:


$url = 'http://www.baidu.com';

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 20); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
$ret = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch);


If you grab the 302 state, it is because in the process of re-grabbing, some jumps need to pass parameters to the next link, and the next link is also set as illegal access if the corresponding parameters are not received.


curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');

The display should be normal.

The above is used to grab the function, which should be almost no problem. You can check the relevant information of CURLOPT_CUSTOMREQUEST.

Use a custom request information instead of "GET" or "HEAD" as an HTTP request. This is useful for executing "DELETE" or other more subtle HTTP requests. Valid values such as "GET", "POST", "CONNECT", etc. That is, do not enter the entire HTTP request here. For example, entering "GET/index. html HTTP/1. 0\ r\ n\ r\ n" is incorrect.


Related articles: