Home > Backend Development > PHP Tutorial > Code for POST data to API interface using CURL method under PHP

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

高洛峰
Release: 2023-03-04 08:02:02
Original
1084 people have browsed it

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

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!

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