当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP下使用CURL方式POST数据至API接口的代码

PHP下使用CURL方式POST数据至API接口的代码

2019年04月14日  | 移动技术网IT编程  | 我要评论

yuskin,一吨柴油多少升,莎士比亚的十四行诗

其实,也比较简单,上代码:

复制代码 代码如下:

<?php     

    $url = 'http://127.0.0.1/test.php';//post指向的链接     
    $data = array(     
        'access_token'=>'thekeyvalue'    
    );     

    $json_data = postdata($url, $data);     
    $array = json_decode($json_data,true);     
    echo '<pre>';print_r($array);     

    function postdata($url, $data)     
    {     
        $ch = curl_init();     
        $timeout = 300;      
        curl_setopt($ch, curlopt_url, $url);    
        curl_setopt($ch, curlopt_referer, "//www.jb51.net/");   //构造来路   
        curl_setopt($ch, curlopt_post, true);     
        curl_setopt($ch, curlopt_postfields, $data);     
        curl_setopt($ch, curlopt_returntransfer, 1);     
        curl_setopt($ch, curlopt_connecttimeout, $timeout);     
        $handles = curl_exec($ch);     
        curl_close($ch);     
        return $handles;     
    }     
?>

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网