Home  >  Article  >  Backend Development  >  How to call interface in php

How to call interface in php

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-09-29 13:19:419358browse

How to call interface in php

When we are doing PHP development, we often need to test the interface, or more conveniently call the interface of some existing modules, get the results and perform subsequent operations. We You can simulate submitting post and get requests through curl to implement these functions.

After that, you can call the interface and get the data through CURL::curl_post($url,$array) or CURL::curl_get($url);.

Related recommendations: "php Getting Started Tutorial"

The following is the encapsulation of curl's post and get:

<?php   
/**  
 * Created by PhpStorm.  
 * User: thinkpad  
 * Date: 2015/7/17 0017  
 * Time: 13:24  
 */  
class Action  
{  
    public static function curl_get($url){  
  
           $testurl = $url;  
           $ch = curl_init();    
           curl_setopt($ch, CURLOPT_URL, $testurl);    
            //参数为1表示传输数据,为0表示直接输出显示。  
           curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
            //参数为0表示不带头文件,为1表示带头文件  
           curl_setopt($ch, CURLOPT_HEADER,0);  
           $output = curl_exec($ch);   
           curl_close($ch);   
           return $output;  
     }
    public static function curl_post($url){  
  
        $curl = curl_init();  
        //设置提交的url  
        curl_setopt($curl, CURLOPT_URL, $url);  
        //设置头文件的信息作为数据流输出  
        curl_setopt($curl, CURLOPT_HEADER, 0);  
        //设置获取的信息以文件流的形式返回,而不是直接输出。  
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
        //设置post方式提交  
        curl_setopt($curl, CURLOPT_POST, 1);
        //执行命令  
        $data = curl_exec($curl);  
        //关闭URL请求  
        curl_close($curl);  
      //获得数据并返回  
        return $data;  
    }  
}

The above is the detailed content of How to call interface in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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