php的curl请求是非常好用的功能,下面简单写个范例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function curl_get($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $response = curl_exec($ch); $head_info = curl_getinfo($ch); if ($head_info['http_code'] != 200){ return false; } return $response; } |