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
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);
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";
}
?>