正在显示
1 个修改的文件
包含
36 行增加
和
0 行删除
| 1 | <?php | 1 | <?php |
| 2 | + | ||
| 3 | +if (!function_exists('http_request')) { | ||
| 4 | +// curl请求 | ||
| 5 | + function http_request($url, $timeout = 30, $header = array()) | ||
| 6 | + { | ||
| 7 | + if (!function_exists('curl_init')) { | ||
| 8 | + throw new Exception('server not install curl'); | ||
| 9 | + } | ||
| 10 | + $ch = curl_init(); | ||
| 11 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
| 12 | + curl_setopt($ch, CURLOPT_HEADER, true); | ||
| 13 | + curl_setopt($ch, CURLOPT_URL, $url); | ||
| 14 | + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); | ||
| 15 | + if (!empty($header)) { | ||
| 16 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $header); | ||
| 17 | + } | ||
| 18 | + $data = curl_exec($ch); | ||
| 19 | + list($header, $data) = explode("\r\n\r\n", $data); | ||
| 20 | + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
| 21 | + if ($http_code == 301 || $http_code == 302) { | ||
| 22 | + $matches = array(); | ||
| 23 | + preg_match('/Location:(.*?)\n/', $header, $matches); | ||
| 24 | + $url = trim(array_pop($matches)); | ||
| 25 | + curl_setopt($ch, CURLOPT_URL, $url); | ||
| 26 | + curl_setopt($ch, CURLOPT_HEADER, false); | ||
| 27 | + $data = curl_exec($ch); | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + if ($data == false) { | ||
| 31 | + curl_close($ch); | ||
| 32 | + } | ||
| 33 | + @curl_close($ch); | ||
| 34 | + | ||
| 35 | + return $data; | ||
| 36 | + } | ||
| 37 | +} |
-
请 注册 或 登录 后发表评论