如何用PHP请求api

<?php

function curls($url , $data=[]){

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    if($data){
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

//get请求
curls('http://www.baidu.com/');

//get带参数请求
curls('http://www.baidu.com?id=1');

//post请求
curls('http://www.baidu.com/',[]);

//post带参数请求
curls('http://www.baidu.com/',['id'=>1]);

 

posted @ 2021-08-05 10:28  &权  阅读(190)  评论(0编辑  收藏  举报