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

angryTom
Release: 2023-02-28 07:18:02
Original
3148 people have browsed it

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;
     }
Copy after login

Then I call it happily:

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

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!

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