Detailed explanation of php get and post methods using curl to call interface methods

伊谢尔伦
Release: 2023-03-12 13:10:01
Original
3114 people have browsed it

The first is the client execution method ApiModel.php:

<?php 
/**
   * 模拟post进行url请求
   * @param string $url
   * @param array $post_data
   */
  function request_post($url = &#39;&#39;,$ispost=true, $post_data = array()) {
    if (empty($url) || empty($post_data)) {
      return false;
    }
    
    $o = "";
    foreach ( $post_data as $k => $v ) 
    { 
      $o.= "$k=" . urlencode( $v ). "&" ;
    }
    $post_data = substr($o,0,-1);
    $key=md5(base64_encode($post_data));
    if($ispost){
      $url=$url;
    }else{
      $url = $url.&#39;?&#39;.$post_data;
    }
    
    
    $curlPost = &#39;key=&#39;.$key;
    header("Content-type: text/html; charset=utf-8");
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$url);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    if($ispost){
      curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
      curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    }
    $data = curl_exec($ch);//运行curl
    curl_close($ch);
    return $data;
  }
  ?>
Copy after login

The client calls the method. You can configure basic information here api.php:

<?php 
require &#39;ApiModel.php&#39;;
function testAction(){
    $url = &#39;接口地址&#39;;
    $post_data[&#39;appid&#39;]    = &#39;10&#39;;
    $post_data[&#39;appkey&#39;]   = &#39;cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ&#39;;
    $post_data[&#39;member_name&#39;] = &#39;zsjs124&#39;;
    $post_data[&#39;password&#39;]  = &#39;123456&#39;;
    $post_data[&#39;email&#39;]  = &#39;zsjs124@126.com&#39;;
    //$post_data = array();
    $res = request_post($url,$ispost=true,$post_data);    
    print_r($res);

  }
testAction();
?>
Copy after login

Server's Interface function test.php:

<?php 
function serverapi(){
  $key=&#39;57173d6ad842d807443ee0db91fed323&#39;;
  if($_GET&&$_GET[&#39;appkey&#39;]==&#39;cmbohpffXVR03nIpkkQXaAA1Vf5nO4nQ&#39;||$_POST&&$_POST[&#39;key&#39;]===$key){
    $arr=array(&#39;name&#39;=>&#39;huanglu&#39;,&#39;password&#39;=>&#39;123456&#39;);
    echo json_encode($arr);
  }else{
    exit(&#39;非法访问!&#39;);
  }
}
serverapi();
?>
Copy after login

The above is the detailed content of Detailed explanation of php get and post methods using curl to call interface methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!