Tips:点此可运行HTML源码

PHP POST数据至远程服务器获取信息

当一个跨域远程页面需要通过$_POST获取参数时,你是使用什么方式传递这些参数并获取响应数据呢?下面为您提供一种PHP Post()方法。

复制代码
View Code
 1 <?php
 2 header("Content-type: text/html; charset=utf-8"); 
 3 function Post($url, $post = null) {
 4     $context = array();
 5     if (is_array($post)) {
 6         ksort($post);
 7         $context['http'] = array (
 8             'timeout'=>60,
 9             'method' => 'POST',
10             'content' => http_build_query($post)
11         );
12     }
13     return file_get_contents($url, false, stream_context_create($context));
14 }
15 
16 $data = array (
17     'type' => 'text',
18     'inputValue' => '哈哈'
19 );
20 $result = Post('http://tool.anzhuoxiazai.com:80//servlet/QRServlet', $data);
21 echo str_replace("src='","src='http://tool.anzhuoxiazai.com/",$result);
22 //End_php
复制代码

 参考文章:file_get_contents 增加超时的时间限制 

 

注:cURL系列提供了更加完备的功能支持,请参考《PHP cURL 应用

 1 function Post($url, $post = null) {
 2     $context = array();
 3     if (is_array($post)) {
 4         ksort($post);
 5         $context['http'] = array (
 6             'timeout'=>60,
 7             'method' => 'POST',
 8             'content' => http_build_query($post)
 9         );
10     }
11     return file_get_contents($url, false, stream_context_create($context));
12 }
posted @   Zjmainstay  阅读(1249)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示