多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內(nèi)最全I(xiàn)T社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > 綜合技術(shù) > php中g(shù)et post請求方法封裝

php中g(shù)et post請求方法封裝

來源:程序員人生   發(fā)布時間:2015-09-09 08:20:56 閱讀次數(shù):3664次
<span style="font-size:18px;">網(wǎng)站上的商城可以搭建ecshop實現(xiàn),微信真?zhèn)€微商城也能夠開發(fā)wap版商城,然后通過鏈接鏈到微信菜單上,這樣實現(xiàn)起來就不需要遠(yuǎn)程調(diào)用數(shù)據(jù)了,但登陸上有個問題,在微信上進(jìn)入微商城在用戶體驗上固然不需要再登陸只需要有微信openid便可。所以有個斟酌是在微信端開發(fā)微商城,所以的數(shù)據(jù)是取自網(wǎng)站商城的,這時候需要遠(yuǎn)程要求數(shù)據(jù)。 有了ihttp_request()方法后,可通過此方法獲得遠(yuǎn)程數(shù)據(jù)</span>
<span style="font-size:18px;"></span><pre name="code" class="php">function ihttp_request($url, $post = '', $extra = array(), $timeout = 60) { $urlset = parse_url($url); if(empty($urlset['path'])) { $urlset['path'] = '/'; } if(!empty($urlset['query'])) { $urlset['query'] = "?{$urlset['query']}"; } if(empty($urlset['port'])) { $urlset['port'] = $urlset['scheme'] == 'https' ? '443' : '80'; } if(function_exists('curl_init') && function_exists('curl_exec')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $urlset['scheme']. '://' .$urlset['host'].($urlset['port'] == '80' ? '' : ':'.$urlset['port']).$urlset['path'].$urlset['query']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); if($post) { curl_setopt($ch, CURLOPT_POST, 1); if (is_array($post)) { $post = http_build_query($post); } curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1'); if (!empty($extra) && is_array($extra)) { $headers = array(); foreach ($extra as $opt => $value) { if (strexists($opt, 'CURLOPT_')) { curl_setopt($ch, constant($opt), $value); } elseif (is_numeric($opt)) { curl_setopt($ch, $opt, $value); } else { $headers[] = "{$opt}: {$value}"; } } if(!empty($headers)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } } $data = curl_exec($ch); $status = curl_getinfo($ch); $errno = curl_errno($ch); $error = curl_error($ch); curl_close($ch); if($errno || empty($data)) { return error(1, $error); } else { return ihttp_response_parse($data); } } $method = empty($post) ? 'GET' : 'POST'; $fdata = "{$method} {$urlset['path']}{$urlset['query']} HTTP/1.1 "; $fdata .= "Host: {$urlset['host']} "; if(function_exists('gzdecode')) { $fdata .= "Accept-Encoding: gzip, deflate "; } $fdata .= "Connection: close "; if (!empty($extra) && is_array($extra)) { foreach ($extra as $opt => $value) { if (!strexists($opt, 'CURLOPT_')) { $fdata .= "{$opt}: {$value} "; } } } $body = ''; if ($post) { if (is_array($post)) { $body = http_build_query($post); } else { $body = urlencode($post); } $fdata .= 'Content-Length: ' . strlen($body) . " {$body}"; } else { $fdata .= " "; } if($urlset['scheme'] == 'https') { $fp = fsockopen('ssl://' . $urlset['host'], $urlset['port'], $errno, $error); } else { $fp = fsockopen($urlset['host'], $urlset['port'], $errno, $error); } stream_set_blocking($fp, true); stream_set_timeout($fp, $timeout); if (!$fp) { return error(1, $error); } else { fwrite($fp, $fdata); $content = ''; while (!feof($fp)) $content .= fgets($fp, 512); fclose($fp); return ihttp_response_parse($content, true); } } function ihttp_response_parse($data, $chunked = false) { $rlt = array(); $pos = strpos($data, " "); $split1[0] = substr($data, 0, $pos); $split1[1] = substr($data, $pos + 4, strlen($data)); $split2 = explode(" ", $split1[0], 2); preg_match('/^(S+) (S+) (S+)$/', $split2[0], $matches); $rlt['code'] = $matches[2]; $rlt['status'] = $matches[3]; $rlt['responseline'] = $split2[0]; $header = explode(" ", $split2[1]); $isgzip = false; $ischunk = false; foreach ($header as $v) { $row = explode(':', $v); $key = trim($row[0]); $value = trim($row[1]); if (is_array($rlt['headers'][$key])) { $rlt['headers'][$key][] = $value; } elseif (!empty($rlt['headers'][$key])) { $temp = $rlt['headers'][$key]; unset($rlt['headers'][$key]); $rlt['headers'][$key][] = $temp; $rlt['headers'][$key][] = $value; } else { $rlt['headers'][$key] = $value; } if(!$isgzip && strtolower($key) == 'content-encoding' && strtolower($value) == 'gzip') { $isgzip = true; } if(!$ischunk && strtolower($key) == 'transfer-encoding' && strtolower($value) == 'chunked') { $ischunk = true; } } if($chunked && $ischunk) { $rlt['content'] = ihttp_response_parse_unchunk($split1[1]); } else { $rlt['content'] = $split1[1]; } if($isgzip && function_exists('gzdecode')) { $rlt['content'] = gzdecode($rlt['content']); } $rlt['meta'] = $data; if($rlt['code'] == '100') { return ihttp_response_parse($rlt['content']); } return $rlt; } function ihttp_response_parse_unchunk($str = null) { if(!is_string($str) or strlen($str) < 1) { return false; } $eol = " "; $add = strlen($eol); $tmp = $str; $str = ''; do { $tmp = ltrim($tmp); $pos = strpos($tmp, $eol); if($pos === false) { return false; } $len = hexdec(substr($tmp, 0, $pos)); if(!is_numeric($len) or $len < 0) { return false; } $str .= substr($tmp, ($pos + $add), $len); $tmp = substr($tmp, ($len + $pos + $add)); $check = trim($tmp); } while(!empty($check)); unset($tmp); return $str; }

$goods = array(
"api_version" =>"1.0",
"goods_id" => "4",
"ac" => "ac",
"act" => "search_goods_detail",
"return_data" => "json",
);
$url = "http://10.92.1.3/api.php"; //這里是10.92.1.2服務(wù)器上調(diào)用1.3上api.php獲得其數(shù)據(jù)
$result = ihttp_request($url,$data);
var_dump($result);
打印出的內(nèi)容是:
array(6) {
["code"]=>
string(3) "200"
["status"]=>
string(2) "OK"
["responseline"]=>
string(15) "HTTP/1.1 200 OK"
["headers"]=>
array(9) {
["Server"]=>
string(5) "nginx"
["Date"]=>
string(19) "Fri, 06 Mar 2015 08"
["Content-Type"]=>
string(24) "text/html; charset=utf⑻"
["Transfer-Encoding"]=>
string(7) "chunked"
["Connection"]=>
string(10) "keep-alive"
["Vary"]=>
string(15) "Accept-Encoding"
["X-Powered-By"]=>
string(10) "PHP/5.3.17"
["Cache-control"]=>
string(7) "private"
["Set-Cookie"]=>
array(2) {
[0]=>
string(55) "ECS_ID=05fdcd0810e735bf2b3b3c8ddb5911d94e319b8b; path=/"
[1]=>
string(47) "ECS[visit_times]=1; expires=Sat, 05-Mar⑵016 00"
}
}
["content"]=>
string(241) "{"result":"success","msg":"","info":{"data_info":[{"goods_id":"1","last_modify":"1423937979"},{"goods_id":"2","last_modify":"1425595831"},{"goods_id":"3","last_modify":"1423937959"},{"goods_id":"4","last_modify":"1423942862"}],"counts":"4"}}"
["meta"]=>
string(625) "HTTP/1.1 200 OK
Server: nginx
Date: Fri, 06 Mar 2015 08:03:41 GMT
Content-Type: text/html; charset=utf⑻
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.3.17
Set-Cookie: ECS_ID=05fdcd0810e735bf2b3b3c8ddb5911d94e319b8b; path=/
Cache-control: private
Set-Cookie: ECS[visit_times]=1; expires=Sat, 05-Mar⑵016 00:03:41 GMT; path=/
打印的內(nèi)容非常詳細(xì),連頭部信息都打出來了,但我們只需要關(guān)心content中的內(nèi)容,這才是我們需要獲得的數(shù)據(jù)
["content"]=>
string(241) "{"result":"success","msg":"","info":{"data_info":[{"goods_id":"1","last_modify":"1423937979"},{"goods_id":"2","last_modify":"1425595831"},{"goods_id":"3","last_modify":"1423937959"},{"goods_id":"4","last_modify":"1423942862"}],"counts":"4"}}"


     
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 18在线| 日本不卡视频在线 | 噜噜影院无毒不卡 | 精品成人在线视频 | 日本护士视频xxxxxwww | www.99视频| 在线中文字幕视频 | 亚洲精品成人图区 | 国产精品一区二区三区免费视频 | 4438x成人网最大色成网站 | 最近的中文字幕手机在线看免费 | 91精品国产91热久久p | 久久精品国产网红主播图片 | 国产日产欧产精品精品推荐在线 | 午夜影院h | freexx性欧美黑人 | 秋霞一级黄色片 | 久久精品国产一区二区三区 | 国产h视频在线观看 | a色在线 | 亚洲精品欧美精品日韩精品 | 第一页在线 | 精品国产免费一区二区三区 | 国产亚洲精品国产福利在线观看 | 欧美黄色片在线观看 | 国产区一区 | 欧美jjzz| 国产尤物在线观看 | 成人午夜视频在线 | 性欧美高清极品猛交 | 在线亚州 | 成人福利片 | 久久精品免视看国产明星 | 三浦惠理子中文字幕在线一区二区 | 日韩高清免费观看 | 亚洲国产成人在线视频 | 亚洲欧美日韩一区 | 在线九色| 亚洲动漫在线观看 | 精品久久久久久 | 韩国jizz |