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 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值