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 @ 2012-08-15 09:02  Zjmainstay  阅读(1232)  评论(0编辑  收藏  举报