如何使用cURL 檢索頁面內容
在從Google 搜尋結果中提取內容時,您可能會遇到諸如和使用cURL 時出現「頁面已移動」錯誤。通常,這些障礙是由於編碼的查詢字串引起的。
要有效檢索所需的內容,請考慮以下 PHP 實作:
<code class="php">/** * Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an * array containing the HTTP server response header fields and content. */ function get_web_page( $url ) { $user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0'; $options = array( CURLOPT_CUSTOMREQUEST => "GET", // set request type post or get CURLOPT_POST => false, // set to GET CURLOPT_USERAGENT => $user_agent, // set user agent CURLOPT_COOKIEFILE => "cookie.txt", // set cookie file CURLOPT_COOKIEJAR => "cookie.txt", // set cookie jar CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => false, // don't return headers CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_ENCODING => "", // handle all encodings CURLOPT_AUTOREFERER => true, // set referer on redirect CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect CURLOPT_TIMEOUT => 120, // timeout on response CURLOPT_MAXREDIRS => 10, // stop after 10 redirects ); $ch = curl_init( $url ); curl_setopt_array( $ch, $options ); $content = curl_exec( $ch ); $err = curl_errno( $ch ); $errmsg = curl_error( $ch ); $header = curl_getinfo( $ch ); curl_close( $ch ); $header['errno'] = $err; $header['errmsg'] = $errmsg; $header['content'] = $content; return $header; }</code>
範例:
<code class="php">// Read a web page and check for errors: $result = get_web_page( $url ); if ( $result['errno'] != 0 ) ... error: bad url, timeout, redirect loop ... if ( $result['http_code'] != 200 ) ... error: no page, no permissions, no service ... $page = $result['content'];</code>
以上是如何使用 cURL 處理 URL 重新導向和頁面移動錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!