<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"; //這裡是10.92.1.2伺服器上呼叫1.3上api.php取得其資料<br>$result = ihttp_request($url,$data);<br>var_dump($result);<br>印出的內容是:<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/<br>["Content-Type"]=><br>string(24) "text/<br>["Content-Type"]=><br>string(24) "text/<br>["Content-Type"]=><br>string(24) "text/<br>["Content-Type"]=><br>string(24) "text/<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=05fdcd0810e735bf2b3b3c8ddb5911d94e3198b; ) "ECS[visit_times]=1; expires=Sat, 05-Mar-2016 00"<br>}<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==f-8🎟 -Encoding: chunked<br>Connection: keep-alive<br>Vary: Accept-Encoding<br>X-Powered-By: PHP/5.3.17<p>Set-Cookie: ECS_ID=05fdcd0810e735bf2b3b3c89bbg3b3c </p>Set-Cookie : ECS[visit_times]=1; expires=Sat, 05-Mar-2016 00:03:41 GMT; path=/<p>印刷的內容非常詳細,連頭部資訊都打出來了,但我們只需要關心content中的內容,這才是我們需要取得的資料</p>["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"}}"🎜<pre class="brush:php;toolbar:false">🎜 以上就介紹了php中get post請求方法封裝,包含了面向的內容,希望對PHP教學有興趣的朋友有幫助。 🎜 🎜 🎜

PHP在現代編程中仍然是一個強大且廣泛使用的工具,尤其在web開發領域。 1)PHP易用且與數據庫集成無縫,是許多開發者的首選。 2)它支持動態內容生成和麵向對象編程,適合快速創建和維護網站。 3)PHP的性能可以通過緩存和優化數據庫查詢來提升,其廣泛的社區和豐富生態系統使其在當今技術棧中仍具重要地位。

在PHP中,弱引用是通過WeakReference類實現的,不會阻止垃圾回收器回收對象。弱引用適用於緩存系統和事件監聽器等場景,需注意其不能保證對象存活,且垃圾回收可能延遲。

\_\_invoke方法允許對象像函數一樣被調用。 1.定義\_\_invoke方法使對象可被調用。 2.使用$obj(...)語法時,PHP會執行\_\_invoke方法。 3.適用於日誌記錄和計算器等場景,提高代碼靈活性和可讀性。

Fibers在PHP8.1中引入,提升了並發處理能力。 1)Fibers是一種輕量級的並發模型,類似於協程。 2)它們允許開發者手動控制任務的執行流,適合處理I/O密集型任務。 3)使用Fibers可以編寫更高效、響應性更強的代碼。

PHP社區提供了豐富的資源和支持,幫助開發者成長。 1)資源包括官方文檔、教程、博客和開源項目如Laravel和Symfony。 2)支持可以通過StackOverflow、Reddit和Slack頻道獲得。 3)開發動態可以通過關注RFC了解。 4)融入社區可以通過積極參與、貢獻代碼和學習分享來實現。

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

WebStorm Mac版
好用的JavaScript開發工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 Linux新版
SublimeText3 Linux最新版

記事本++7.3.1
好用且免費的程式碼編輯器