I'm trying to create a REST API. The API isn't a problem and have one working that I can call from the URL. Trying to get the JSON data from another page but can't seem to get it working.
I have a jQuery version that pulls the data just fine
<script>
$(function() {
$.getJSON("rest.php?data=woot", function(data) {
console.log(data);
});
});
</script>
The console.log gives me the anticipated data
Object { index="woot", value="yay"}
so the REST itself is working (URL and jQuery verify this)
but I would like to use a PHP option. I know you have to use a cURL command but having a problem getting it to work. Here are the latest options tried but none seem to pull over the data. my php.ini file is set correctly (based on other posts and tutorials).
<?php
//$json = file_get_contents('php://input');
//$json = file_get_contents('/wip/_test/rest.php?data=woot');
$url = 'rest.php?data=woot';
$ch = curl_init();
//http://php.net/manual/en/function.curl-setopt.php
//$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_TIMEOUT, 5);
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
//for debugging?
curl_setopt($ch, CURLOPT_VERBOSE, true);
$data = curl_exec($ch);
curl_close($ch);
$obj = json_decode($data);
echo '<pre>curl:<br>~';print_r($obj); echo '~</pre>';
echo $data . '+++<br>';
$json = file_get_contents($url);
// true, put data into array instead of object
$array = json_decode($json, true);
echo '<pre>json:<br>~';print_r($array); echo '~</pre>';
$rest_json = file_get_contents("php://".$url);
$rest_vars = json_decode($rest_json, true);
echo '<pre>rest_vars:<br>~';print_r($rest_vars); echo '~</pre>';
echo _isCurl() . '---<br>';
echo '<pre>Curl version:<br>~';print_r(curl_version()); echo '~</pre>';
function _isCurl() {
return function_exists('curl_version');
}
?>
rest.php file
<?php
$index = $_GET['data'];
//test DB
$database = Array(
'woot' => 'yay',
'foo' => 'poopoo',
'ha' => 42
);
if(isset($database[strtolower($index)]))
respondJSON(200, "WOOT", $index, $database[strtolower($index)]);
else
respondJSON(200, "D-OH!!!", $index, "");
function respondJSON($status, $msg, $index, $value) {
//header("content-type:application/json");
//header("HTTP/1.1 $status $msg", true, 200);
//echo $status."~\n".$msg."~\n".$index."~\n".$value."\n";
$resp['index'] = $index;
$resp['value'] = $value;
echo json_encode($resp);
}
?>
cURL and 2 flavors of the file_get_contents option. all of these echos out an empty string. I'm not sure what's wrong. could it maybe be a php.ini setting i'm missing?
cURL is active, here is the version output from the last part
Curl version:
~Array
(
[version_number] => 464896
[age] => 3
[features] => 34493
[ssl_version_number] => 0
[version] => 7.24.0
[host] => x86_64-unknown-linux-gnu
[ssl_version] => OpenSSL/1.0.0
[libz_version] => 1.2.3
[protocols] => Array
(
[0] => dict
[1] => file
[2] => ftp
[3] => ftps
[4] => gopher
[5] => http
[6] => https
[7] => imap
[8] => imaps
[9] => ldap
[10] => ldaps
[11] => pop3
[12] => pop3s
[13] => rtsp
[14] => scp
[15] => sftp
[16] => smtp
[17] => smtps
[18] => telnet
[19] => tftp
)
)~
Thank you