How to get JSON data from Rest API by PHP Curl?

后端 未结 2 812
悲&欢浪女
悲&欢浪女 2021-01-24 03:18

I have a Rest api which I can access by this url: \"http://127.0.0.1:8000/api/thesis/?format=json\". Now I want to get the JSON data from it. For connecting to the api I tried t

相关标签:
2条回答
  • 2021-01-24 03:52

    I think you should use GET method of curl like this

    $service_url = "http://127.0.0.1:8000/api/thesis/?format=json";
    $curl = curl_init($service_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    //execute the session
    $curl_response = curl_exec($curl);
    //finish off the session
    curl_close($curl);
    $curl_jason = json_decode($curl_response, true);
    print_r($curl_jason);
    
    0 讨论(0)
  • 2021-01-24 03:56

    Use the below mentioned code snippet to fetch data from a REST API using PHP curl

     <?php
        function _isCurl(){
            return function_exists('curl_version');
        }    
        if (_iscurl()){
            //curl is enabled
            $url = "http://testDomainName/restAPI.php?id=123&amt=100&jsonp=?";    
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);                               
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $output = curl_exec($ch);
            curl_close($ch);
            print_r($output);
           // Curl operations finished            
        }
        else{
            echo "CURL is disabled";
        }
        ?>
    
    0 讨论(0)
提交回复
热议问题