Home >php教程 >PHP开发 >Code for POST data to API interface using CURL method under PHP

Code for POST data to API interface using CURL method under PHP

高洛峰
高洛峰Original
2016-12-23 15:54:211131browse

Actually, it’s relatively simple, here’s the code:

<?php      

    $url = &#39;http://127.0.0.1/test.php&#39;;//POST指向的链接      
    $data = array(      
        &#39;access_token&#39;=>&#39;thekeyvalue&#39;     
    );      

    $json_data = postData($url, $data);      
    $array = json_decode($json_data,true);      
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;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;      
    }      
?>


For more code-related articles on using CURL to POST data to the API interface under PHP, please pay attention to 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