1//get获取 2function ihttp_get($url) { 3 return ihttp_request($url); 4} 5 6//post获取 7function ihttp_post($url, $data) { 8 $headers = array('Content-Type' => 'application/x-www-form-urlencoded'); 9 return ihttp_request($url, $data, $headers); 10} 11 12function ihttp_request($url, $post = '', $extra = array(), $timeout = 60) { 13 $urlset = parse_url($url); 14 if(empty($urlset['path'])) { 15 $urlset['path'] = '/'; 16 } 17 if(!empty($urlset['query'])) { 18 $urlset['query'] = "?{$urlset['query']}"; 19 } 20 if(empty($urlset['port'])) { 21 $urlset['port'] = $urlset['scheme'] == 'https' ? '443' : '80'; 22 } 23 24 if(function_exists('curl_init') && function_exists('curl_exec')) { 25 $ch = curl_init(); 26 curl_setopt($ch, CURLOPT_URL, $urlset['scheme']. '://' .$urlset['host'].($urlset['port'] == '80' ? '' : ':'.$urlset['port']).$urlset['path'].$urlset['query']); 27 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 28 curl_setopt($ch, CURLOPT_HEADER, 1); 29 if($post) { 30 curl_setopt($ch, CURLOPT_POST, 1); 31 if (is_array($post)) { 32 $post = http_build_query($post); 33 } 34 curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 35 } 36 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 37 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); 38 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 39 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 40 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1'); 41 if (!empty($extra) && is_array($extra)) { 42 $headers = array(); 43 foreach ($extra as $opt => $value) { 44 if (strexists($opt, 'CURLOPT_')) { 45 curl_setopt($ch, constant($opt), $value); 46 } elseif (is_numeric($opt)) { 47 curl_setopt($ch, $opt, $value); 48 } else { 49 $headers[] = "{$opt}: {$value}"; 50 } 51 } 52 if(!empty($headers)) { 53 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 54 } 55 } 56 $data = curl_exec($ch); 57 $status = curl_getinfo($ch); 58 $errno = curl_errno($ch); 59 curl_close($ch); 60 if($errno || empty($data)) { 61 return false; 62 } else { 63 return ihttp_response_parse($data); 64 } 65 } 66 $method = empty($post) ? 'GET' : 'POST'; 67 $fdata = "{$method} {$urlset['path']}{$urlset['query']} HTTP/1.1\r\n"; 68 $fdata .= "Host: {$urlset['host']}\r\n"; 69 if(function_exists('gzdecode')) { 70 $fdata .= "Accept-Encoding: gzip, deflate\r\n"; 71 } 72 $fdata .= "Connection: close\r\n"; 73 if (!empty($extra) && is_array($extra)) { 74 foreach ($extra as $opt => $value) { 75 if (!strexists($opt, 'CURLOPT_')) { 76 $fdata .= "{$opt}: {$value}\r\n"; 77 } 78 } 79 } 80 $body = ''; 81 if ($post) { 82 if (is_array($post)) { 83 $body = http_build_query($post); 84 } else { 85 $body = urlencode($post); 86 } 87 $fdata .= 'Content-Length: ' . strlen($body) . "\r\n\r\n{$body}"; 88 } else { 89 $fdata .= "\r\n"; 90 } 91 if($urlset['scheme'] == 'https') { 92 $fp = fsockopen('ssl://' . $urlset['host'], $urlset['port']); 93 } else { 94 $fp = fsockopen($urlset['host'], $urlset['port']); 95 } 96 stream_set_blocking($fp, true); 97 stream_set_timeout($fp, $timeout); 98 if (!$fp) { 99 return false; 100 } else { 101 fwrite($fp, $fdata); 102 $content = ''; 103 while (!feof($fp)) 104 $content .= fgets($fp, 512); 105 fclose($fp); 106 return ihttp_response_parse($content, true); 107 } 108} 109 110function ihttp_response_parse($data, $chunked = false) { 111 $rlt = array(); 112 $pos = strpos($data, "\r\n\r\n"); 113 $split1[0] = substr($data, 0, $pos); 114 $split1[1] = substr($data, $pos + 4, strlen($data)); 115 116 $split2 = explode("\r\n", $split1[0], 2); 117 preg_match('/^(\S+) (\S+) (\S+)$/', $split2[0], $matches); 118 $rlt['code'] = $matches[2]; 119 $rlt['status'] = $matches[3]; 120 $rlt['responseline'] = $split2[0]; 121 $header = explode("\r\n", $split2[1]); 122 $isgzip = false; 123 $ischunk = false; 124 foreach ($header as $v) { 125 $row = explode(':', $v); 126 $key = trim($row[0]); 127 $value = trim($row[1]); 128 if (is_array($rlt['headers'][$key])) { 129 $rlt['headers'][$key][] = $value; 130 } elseif (!empty($rlt['headers'][$key])) { 131 $temp = $rlt['headers'][$key]; 132 unset($rlt['headers'][$key]); 133 $rlt['headers'][$key][] = $temp; 134 $rlt['headers'][$key][] = $value; 135 } else { 136 $rlt['headers'][$key] = $value; 137 } 138 if(!$isgzip && strtolower($key) == 'content-encoding' && strtolower($value) == 'gzip') { 139 $isgzip = true; 140 } 141 if(!$ischunk && strtolower($key) == 'transfer-encoding' && strtolower($value) == 'chunked') { 142 $ischunk = true; 143 } 144 } 145 if($chunked && $ischunk) { 146 $rlt['content'] = ihttp_response_parse_unchunk($split1[1]); 147 } else { 148 $rlt['content'] = $split1[1]; 149 } 150 if($isgzip && function_exists('gzdecode')) { 151 $rlt['content'] = gzdecode($rlt['content']); 152 } 153 154 $rlt['meta'] = $data; 155 if($rlt['code'] == '100') { 156 return ihttp_response_parse($rlt['content']); 157 } 158 return $rlt; 159} 160 161function ihttp_response_parse_unchunk($str = null) { 162 if(!is_string($str) or strlen($str) < 1) { 163 return false; 164 } 165 $eol = "\r\n"; 166 $add = strlen($eol); 167 $tmp = $str; 168 $str = ''; 169 do { 170 $tmp = ltrim($tmp); 171 $pos = strpos($tmp, $eol); 172 if($pos === false) { 173 return false; 174 } 175 $len = hexdec(substr($tmp, 0, $pos)); 176 if(!is_numeric($len) or $len < 0) { 177 return false; 178 } 179 $str .= substr($tmp, ($pos + $add), $len); 180 $tmp = substr($tmp, ($len + $pos + $add)); 181 $check = trim($tmp); 182 } while(!empty($check)); 183 unset($tmp); 184 return $str; 185}
PHP CURL方法,GET&POST请求。
Wesley13
2021-10-11
1224 0 0
点赞
收藏
评论区
加载中...