当前位置:首页 > 综合实例 > 列表

php模拟post提交请求调用接口示例解析

发布:smiling 来源: PHP粉丝网  添加日期:2022-03-21 11:55:16 浏览: 评论:0 

这篇文章主要介绍了php模拟post提交请求调用接口示例解析,文章通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

php模拟post提交请求,调用接口:

  1. /** 
  2.  * 模拟post进行url请求 
  3.  * @param string $url 
  4.  * @param string $param 
  5.  */ 
  6.  function request_post($url = ''$param = '') { 
  7.  if (emptyempty($url) || emptyempty($param)) { 
  8.   return false; 
  9.  } 
  10.    
  11.  $postUrl = $url
  12.  $curlPost = $param
  13.  $ch = curl_init();//初始化curl 
  14.  curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页 
  15.  curl_setopt($ch, CURLOPT_HEADER, 0);//设置header 
  16.  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 
  17.  curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 
  18.  curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); 
  19.  $data = curl_exec($ch);//运行curl 
  20.  curl_close($ch); 
  21.    
  22.  return $data
  23.  } 

这是方法,下面是具体的调用案例。

  1. function testAction(){ 
  2.  $url = 'http://mobile.jschina.com.cn/jschina/register.php'
  3.  $post_data['appid'] = '10'
  4.  $post_data['appkey'] = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ'
  5.  $post_data['member_name'] = 'zsjs123'
  6.  $post_data['password'] = '123456'
  7.  $post_data['email'] = 'zsjs123@126.com'
  8.  $o = ""
  9.  foreach ( $post_data as $k => $v )  
  10.  {  
  11.   $o.= "$k=" . urlencode( $v ). "&" ; 
  12.  } 
  13.  $post_data = substr($o,0,-1); 
  14.  
  15.  $res = $this->request_post($url$post_data);  
  16.  print_r($res); 
  17.  
  18.  } 

这样就提交请求,并且获取请求结果了,一般返回的结果是json格式的。

这里的post是拼接出来的。

也可以改造成下面的方式。

  1. /** 
  2.  * 模拟post进行url请求 
  3.  * @param string $url 
  4.  * @param array $post_data 
  5.  */ 
  6.  function request_post($url = ''$post_data = array()) { 
  7.  if (emptyempty($url) || emptyempty($post_data)) { 
  8.   return false; 
  9.  } 
  10.    
  11.  $o = ""
  12.  foreach ( $post_data as $k => $v )  
  13.  {  
  14.   $o.= "$k=" . urlencode( $v ). "&" ; 
  15.  } 
  16.  $post_data = substr($o,0,-1); 
  17.  
  18.  $postUrl = $url
  19.  $curlPost = $post_data
  20.  $ch = curl_init();//初始化curl 
  21.  curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页 
  22.  curl_setopt($ch, CURLOPT_HEADER, 0);//设置header 
  23.  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 
  24.  curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 
  25.  curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); 
  26.  $data = curl_exec($ch);//运行curl 
  27.  curl_close($ch); 
  28.    
  29.  return $data
  30.  } 

将拼接也封装了起来,这样调用的时候就更简洁了。

  1. function testAction(){ 
  2.  $url = 'http://mobile.jschina.com.cn/jschina/register.php'
  3.  $post_data['appid'] = '10'
  4.  $post_data['appkey'] = 'cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ'
  5.  $post_data['member_name'] = 'zsjs124'
  6.  $post_data['password'] = '123456'
  7.  $post_data['email'] = 'zsjs124@126.com'
  8.  //$post_data = array(); 
  9.  $res = $this->request_post($url$post_data);  
  10.  print_r($res);
  11.  }

Tags: php模拟post php请求调用接口

分享到: