<span style="font-size:18px;">网站上的商城可以搭建ecshop实现,微信端的微商城也可以开发wap版商城,然后通过链接链到微信菜单上,这样实现起来就不需要远程调用数据了,但登陆上有个问题,在微信上进入微商城在用户体验上当然不需要再登陆只需要有微信openid即可。所以有个考虑是在微信端开发微商城,所以的数据是取自网站商城的,这时需要远程请求数据。 有了ihttp_request()方法后,可通过此方法获取远程数据</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\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) <br>$goods = array(<br>"api_version" =>"1.0",<br>"goods_id" => "4",<br>"ac" => "ac",<br>"act" => "search_goods_detail" ,<br>"return_data" => "json",<br>);<br>$url = "http://10.92.1.3/api.php"; //Here is the api.php on 1.3 on the 10.92.1.2 server to get its Data<br>$result = ihttp_request($url,$data);<br>var_dump($result);<br>The printed content is:<br>array(6) {<br>["code"]=><br>string(3) "200 "<br>["status"]=><br>string(2) "OK"<br>["responseline"]=><br>string(15) "HTTP/1.1 200 OK"<br>["headers"]=><br>array (9) {<br>["Server"]=><br>string(5) "nginx"<br>["Date"]=><br>string(19) "Fri, 06 Mar 2015 08"<br>["Content-Type" ]=><br>string(24) "text/html; charset=utf-8"<br>["Transfer-Encoding"]=><br>string(7) "chunked"<br>["Connection"]=><br>string (10) "keep-alive"<br>["Vary"]=><br>string(15) "Accept-Encoding"<br>["X-Powered-By"]=><br>string(10) "PHP/5.3. 17"<br>["Cache-control"]=><br>string(7) "private"<br>["Set-Cookie"]=><br>array(2) {<br>[0]=><br>string(55) "ECS_ID = 05FDCD0810E735BF2B3B3C8ddb5911d94e319b8b; PATH =/" <br> [1] = & GT; <br>String (47) "ECS [VISIT_TIMES] = 1; Expires = Sat, 05-MAR-2016 00 "}} <br>} <br> [" content "] =><br>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"}}"<br>["meta"]=><br>string(625) "HTTP/1.1 200 OK<br>Server: nginx<br>Date: Fri, 06 Mar 2015 08:03:41 GMT<br>Content-Type: text/html; charset=utf-8<br>Transfer-Encoding: chunked<br>Connection: keep-alive<br>Vary: Accept-Encoding<br>X-Powered-By: PHP/5.3.17 <br>Set-Cookie: ECS_ID=05fdcd0810e735bf2b3b3c8ddb5911d94e319b8b; path=/ <br> The printed content is very detailed, even the header information is typed out, but we only need to care about the content in the content. This is the data we need to obtain<br>["content"]=><br>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"}}"<br><pre class="brush:php;toolbar:false">
The above introduces the encapsulation of the get post request method in PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.