A-A+

PHP页面文件怎么发送POST请求

2018年01月05日 PHP技术文章 暂无评论 阅读 0 views 次

在做PHP接口的时候,是需要使用GET或者POST提交数据,然后远程服务器返回return值,提交数据我们通常使用的是form表单或者jquery的方式进行提交,可以同步获取到接口返回的数据,不过最近遇到一个需要PHP文件页面直接POST过去的问题,这样的操作其实是很少的,通常能解决的话都是直接form表单提交过去,既然遇到了,哪就来解决一下吧。

需求是这样的,A网站是购物网站,B网站是一个普通站点,且有用户余额,同步登陆注册的功能已经做好了,在A网站可以关联到B网站的会员ID,但是余额是不关联的,也就是说是两个独立系统,两个数据库,现在需要使用B网站的余额去购买A网站的商品,因为前面购物站点所有的逻辑都是做好的,所以在提交的时候,只能在PHP中POST数据过去。

知道了需求,问题就不难解决了,只要在PHP页面POST数据就可以了,这里有三个方法,第一种是较为常用的方法。

  1. /**  
  2.  * 发送post请求  
  3.  * @param string $url 请求地址  
  4.  * @param array $post_data post键值对数据  
  5.  * @return string  
  6.  */    
  7. function send_post($url$post_data) {    
  8.     
  9.   $postdata = http_build_query($post_data);    
  10.   $options = array(    
  11.     'http' => array(    
  12.       'method' => 'POST',    
  13.       'header' => 'Content-type:application/x-www-form-urlencoded',    
  14.       'content' => $postdata,    
  15.       'timeout' => 15 * 60 // 超时时间(单位:s)    
  16.     )    
  17.   );    
  18.   $context = stream_context_create($options);    
  19.   $result = file_get_contents($url, false, $context);    
  20.     
  21.   return $result;    
  22. }    
  23.     
  24. //使用方法    
  25. $post_data = array(    
  26.   'username' => 'stclair2201',    
  27.   'password' => 'handan'    
  28. );    
  29. send_post('http://www.xiariboke.net', $post_data);    
  30. <?php    
  31. /**  
  32.  * Socket版本  
  33.  * 使用方法:  
  34.  * $post_string = "app=socket&version=beta";  
  35.  * request_by_socket('www.xiariboke.net', '/restServer.php', $post_string);  
  36.  */    
  37. function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {    
  38.   $socket = fsockopen($remote_server$port$errno$errstr$timeout);    
  39.   if (!$socketdie("$errstr($errno)");    
  40.   fwrite($socket"POST $remote_path HTTP/1.0");    
  41.   fwrite($socket"User-Agent: Socket Example");    
  42.   fwrite($socket"HOST: $remote_server");    
  43.   fwrite($socket"Content-type: application/x-www-form-urlencoded");    
  44.   fwrite($socket"Content-length: " . (strlen($post_string) + 8) . "");    
  45.   fwrite($socket"Accept:*/*");    
  46.   fwrite($socket"");    
  47.   fwrite($socket"mypost=$post_string");    
  48.   fwrite($socket"");    
  49.   $header = "";    
  50.   while ($str = trim(fgets($socket, 4096))) {    
  51.     $header .= $str;    
  52.   }    
  53.     
  54.   $data = "";    
  55.   while (!feof($socket)) {    
  56.     $data .= fgets($socket, 4096);    
  57.   }    
  58.     
  59.   return $data;    
  60. }    
  61. ?>    
  62.     
  63. <?php    
  64. /**    
  65.  * Curl版本    
  66.  * 使用方法:    
  67.  * $post_string = "app=request&version=beta";    
  68.  * request_by_curl('http://www.xiariboke.net/restServer.php', $post_string);    
  69.  */    
  70. function request_by_curl($remote_server$post_string) {    
  71.   $ch = curl_init();    
  72.   curl_setopt($ch, CURLOPT_URL, $remote_server);    
  73.   curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);    
  74.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
  75.   curl_setopt($ch, CURLOPT_USERAGENT, "qianyunlai.com's CURL Example beta");    
  76.   $data = curl_exec($ch);    
  77.   curl_close($ch);    
  78.     
  79.   return $data;    
  80. }    
  81. ?>  

其中第一种方法比较简单,且不需要在php.ini中进行配置,这里要注意的是服务器端的接口文件需要 echo 打印出来数据才可以返回,比如我这里使用的是tp系统,接口的文件代码如下:

  1. //返回会员余额log  
  2. public function member_money_pay(){  
  3.        
  4.     $id = I('post.member_id');  
  5.     $money = I('post.money');  
  6.     $order_sn =I('post.order_sn');  
  7.     if (!emptyempty($id) && !emptyempty($money)) $res = D("MemberAccount")->updateUserAccount($id,$money,'xiaohongbao_reduce_success');  
  8.     emptyempty($res) ? $res = 0 : $res =1;  
  9.     emptyempty($res) ? $is_pay = 0 : $is_pay =1;  
  10.     $data = array(  
  11.         'member_id' => $id,  
  12.         'money' => $money,  
  13.         'order_sn' => $order_sn,  
  14.         'is_pay' => $is_pay,  
  15.         'create_time' => date('Y-m-d H:i:s')  
  16.     );//www.xiariboke.net  
  17.     M('XiaohongbaoOrder')->add($data);  
  18.     echo $res;  
  19. }   

虽然这里使用的是 echo $res; 但在php页面级是可以传递的,返回的也是 $res,经测试,可以直接使用。

标签:

给我留言