我有一个 Rest api,我可以通过这个 url 访问它:“ http://127.0.0.1:8000/api/thesis/?format=json ”。现在我想从中获取 JSON 数据。为了连接到 api,我尝试使用 PHP-Curl,如下所示。但我得到NULL!(这是我第一次做 php 任何帮助都会很棒!)
<?php
$service_url = "http://127.0.0.1:8000/api/thesis/?format=json";
//initialize a curl session
$curl = curl_init();
//set options for the transfer
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_URL, $service_url);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
//execute the session
$curl_response = curl_exec($curl);
//finish off the session
curl_close($curl);
$curl_jason = var_dump(json_decode($curl_response, true));
print_r($curl_jason);
echo $curl_jason;
?>