1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| <?php namespace Util; class Curl {
public static function post($url, $header = null, $data = null, $isHeader = false) { return self::curl('post', $url, $header, $data, $isHeader); }
public static function put($url, $header = null, $data = null, $isHeader = false) { return self::curl('put', $url, $header, $data, $isHeader); }
public static function get($url, $header = null, $data = null, $isHeader = false) { if ($data) { if (is_array($data)) { $data = http_build_query($data); } if (strpos($url, '?') === false) { $url = $url . '?' . $data; } else { $url = $url . '&' . $data; } } return self::curl('get', $url, $header, null, $isHeader); }
public static function curl($method, $url, $header = null, $data = null, $isHeader = false) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); if ($header) { curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if (stripos($url, "https://") !== FALSE) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); }
if ($data) { if (is_array($data)) { $sets = array(); foreach ($data as $key => $val) { $sets[] = $key . '=' . urlencode($val); } $data = implode('&', $sets); } curl_setopt($ch, CURLOPT_POSTFIELDS, $data); }
$method = strtolower($method); if ('post' == $method) { curl_setopt($ch, CURLOPT_POST, true); } elseif ('put' == $method) { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); }
if ($isHeader) { curl_setopt($ch, CURLOPT_HEADER, 1); }
$output = curl_exec($ch); if ($output === false) { var_dump(curl_error($ch)); } curl_close($ch); return $output; } }
|