Home  >  Article  >  Backend Development  >  How to call the php interface that someone else has prepared?

How to call the php interface that someone else has prepared?

angryTom
angryTomOriginal
2019-10-30 17:47:403137browse

How to call the php interface that someone else has prepared?

phpHow to call the interface when others have done it

Calling the interface is to simulate http requests, because it is different from the front-end , http requests can be initiated directly through ajax or other methods, and the backend has to simulate this request through curl in order to achieve the same effect as the front end.

Encapsulate it:

public static function curl_post($url,$array){
        $curl = curl_init();
        //设置提交的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        //获取数据不直接输出
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post数据
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($array));
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //获得数据并返回
        return $data;
     }

Then I call it happily:

$array = array('open_id'=>$openid);
$url = dr_var("verify");
$res = $this->curl_post($url,$array);

Here is a verify interface of system A that is simulated in the interface of system B for review. User identity, and then determines what system B interface returns based on the audit results.

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of How to call the php interface that someone else has prepared?. 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