Get Post request method encapsulation in PHP

Source: Internet
Author: User

<span style= "FONT-SIZE:18PX;" > website on the Mall can be built ecshop implementation, the end of the micro-mall can also develop a WAP version of the mall, and then through the link to the menu, so that it does not need to remotely call data, but landing on a problem, On the go to micro-mall in the user experience of course no need to login only need to have OpenID. So there is a consideration is the development of the micro-mall, so the data is taken from the site store, the need to remotely request data. With the Ihttp_request () method, you can obtain remote data by using this method </span>
<span style= "FONT-SIZE:18PX;"  ></span><pre name= "code" class= "PHP" >function ihttp_request ($url, $post = ', $extra = Array (), $timeout = {$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_numer IC ($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\r\n"; $fdata. = "Host: {$urlset [' Host ']} \ r \ n ";" if (Function_exists (' Gzdecode ')) {$fdata. = "Accept-encoding:gzip, deflate\r\n";} $fdata. = "ConnectioN:close\r\n ", if (!empty ($extra) && Is_array ($extra)) {foreach ($extra as $opt + $value) {if (!strexists ($opt, ' Curlopt_ ') {$fdata. = "{$opt}: {$value}\r\n";}}} $body = ", if ($post) {if (Is_array ($post)) {$body = Http_build_query ($post);} else {$body = UrlEncode ($post);} $fdata. = ' Content-length: '. Strlen ($body). "\r\n\r\n{$body}";} else {$fdata. = "\ r \ n";}  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), fclose ($FP); return Ihttp_response_parse ($content , true);}} function Ihttp_response_parse ($data, $chunked = False) {$RLT = array (); $pos = Strpos ($data, "\r\n\r\n"); $split 1[0] = subst R ($data, 0, $pos); $split 1[1] = substr ($data, $pos + 4, strlen ($data)); $Split2 = Explode ("\ r \ n", $split 1[0], 2);p Reg_match ('/^ (\s+) (\s+) (\s+) $/', $split 2[0], $matches); $RLT [' code '] = $ MATCHES[2]; $rlt [' status '] = $matches [3]; $RLT [' responseline '] = $split 2[0]; $header = explode ("\ r \ n", $split 2[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 = t Rue;}} if ($chunked && $ischunk) {$rlt [' content '] = Ihttp_response_parse_unchunk ($split 1[1]);} else {$rlt [' content '] = $spliT1[1];} if ($isgzip && function_exists (' Gzdecode ')) {$RLT [' content '] = Gzdecode ($rlt [' content ']);} $RLT [' meta '] = $data, if ($rlt [' code '] = = ') {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 = "\ r \ n"; $add = strlen ($eol); $tmp = $str; $str = ';d o {$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"; Here is the 10.92.1.2 server on call 1.3 on api.php to get its data
$result = Ihttp_request ($url, $data);
Var_dump ($result);
The printed content is:
Array (6) {
["Code"]=>
String (3) "200"
["Status"]=>
String (2) "OK"
["Responseline"]=>
String ("http/1.1-OK")
["Headers"]=>
Array (9) {
["Server"]=>
String (5) "Nginx"
["Date"]=>
String "Fri, Mar 2015 08"
["Content-type"]=>
String ("text/html; Charset=utf-8 "
["Transfer-encoding"]=>
String (7) "chunked"
["Connection"]=>
String (Ten) "Keep-alive"
["Vary"]=>
String ("Accept-encoding")
["X-powered-by"]=>
String (Ten) "php/5.3.17"
["Cache-control"]=>
String (7) "Private"
["Set-cookie"]=>
Array (2) {
[0]=>
String "ECS_ID=05FDCD0810E735BF2B3B3C8DDB5911D94E319B8B; path=/"
[1]=>
String (*) "ecs[visit_times]=1; Expires=sat, 05-mar-2016 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 OK
Server:nginx
Date:fri, Mar 08:03:41 GMT
content-type:text/html; Charset=utf-8
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-2016 00:03:41 GMT; path=/
The printed content is very detailed, even the header information is typed, but we only need to care about content, this is what we need to get the data
["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 "}"

Get Post request method encapsulation in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.