encapsulates a Curl Crawl Web page function, in the local test no problem, when placed on the testing server, if through the browser access to execute, most of the time the function returned by the HTTP status code return 0 , error message ' Error:name Lookup timed out ', the case of extreme Idol returns 200 success, but if executed directly on the test server with the command line, 100% succeeds. The code is as follows:
static public Function Curlget ($url, $data = Array (), $header = Array (), $timeout = 3, $port = +) {$is _ssl = substr ($url, 0, 5) = = ' https '? 1:0; $ch = Curl_init (); if (!empty ($data)) {$data = Is_array ($data)? Http_build_query ($data): $data; $url. = (Strpos ($url, '? ')? ' & ': "?") . $data; } curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, true); curl_setopt ($ch, Curlopt_customrequest, ' GET '); curl_setopt ($ch, curlopt_followlocation,1); Whether to crawl the page after the jump curl_setopt ($ch, Curlopt_connecttimeout, 5); curl_setopt ($ch, Curlopt_timeout, $timeout); curl_setopt ($ch, curlopt_post, 0); curl_setopt ($ch, Curlopt_port, $port); curl_setopt ($ch, Curlopt_httpheader, $header); curl_setopt ($ch, Curlopt_referer, $url); curl_setopt ($ch, Curlopt_useragent, Self::url2useragent ($url)); if ($is _ssl) {curl_setopt ($ch, CURLopt_ssl_verifypeer, false); Skip certificate Check curl_setopt ($ch, Curlopt_ssl_verifyhost, true); Check the existence of the SSL encryption algorithm from the certificate} $result = Array (); $result [' result '] = curl_exec ($ch); $result [' http_code '] = Curl_getinfo ($ch, Curlinfo_http_code); if (0! = Curl_errno ($ch)) {$result [' error '] = "error:\n". Curl_error ($ch); } curl_close ($ch); return $result; }
我个人感觉和代码应该关系不大,不知是哪的问题。望各路大神不吝赐教,指点迷津,不胜感激。