: This article mainly introduces the getpost request method encapsulation in php. if you are interested in the PHP Tutorial, refer to it.
Ecshop can be built on the Mall on the website, and the micro-mall on the end can also develop the wap version mall, and then link to the menu through the link, so that data does not need to be remotely called, however, there is a problem with logging on to the micro-mall. of course, you do not need to log on to the micro-mall any more than openid. Therefore, one consideration is to develop a micro-mall on the client. Therefore, the data is taken from the website Mall. in this case, you need to remotely request data. You can use the ihttp_request () method to obtain remote data.
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\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, 512);fclose($fp);return ihttp_response_parse($content, true);}}function ihttp_response_parse($data, $chunked = false) {$rlt = array();$pos = strpos($data, "\r\n\r\n");$split1[0] = substr($data, 0, $pos);$split1[1] = substr($data, $pos + 4, strlen($data));$split2 = explode("\r\n", $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("\r\n", $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 = "\r\n";$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"; // call api. php on the server 10.92.1.2 to obtain 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 (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; charsets = utf-8"
["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-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 200 OK
Server: nginx
Date: Fri, 06 Mar 2015 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 = 05fdcd0810e735bf2b3c8ddb5911d94e319b8b; 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 printed, but we only need to care about the content, which is the data we need to obtain.
["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 "}}"
The above describes the get post request method encapsulation in php, including some content, and hope to be helpful to friends who are interested in PHP tutorials.