Last active
November 18, 2015 10:11
-
-
Save forecho/378b82b1819180ac7295 to your computer and use it in GitHub Desktop.
封装 curl 类
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function httpClient($url, $data = FALSE, $header = FALSE, $timeout = 30) | |
| { | |
| if (!strstr($url, 'http://') && !strstr($url, 'https://')) { | |
| $url = 'http://' . $url; | |
| } | |
| $curl = curl_init(); | |
| curl_setopt($curl, CURLOPT_URL, $url); | |
| if (!empty($header)) { | |
| curl_setopt($curl, CURLOPT_HEADER, True); | |
| curl_setopt($curl, CURLOPT_HTTPHEADER, $header); | |
| } else { | |
| curl_setopt($curl, CURLOPT_HEADER, False); | |
| } | |
| if (!empty($data)) { | |
| curl_setopt($curl, CURLOPT_POST, True); | |
| curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | |
| } else { | |
| curl_setopt($curl, CURLOPT_POST, False); | |
| } | |
| curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
| curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); | |
| //执行 | |
| $result = curl_exec($curl); | |
| $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
| if ($http_code == 200) { | |
| return $result; | |
| } | |
| if (curl_errno($curl)) { | |
| echo 'Curl error: ' . curl_error($curl); | |
| return false; | |
| } | |
| curl_close($curl); | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment