当前位置:首页 > PHP教程 > PHP高级教程

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

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

复制代码 代码如下:

<?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, "http://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;     
    }     
?>



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!