0

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

Viking NM
  • 392
  • 3
  • 17

1 Answers1

0

You shouldn't need to make a curl call to your own server. Just require in your file and you could use output buffering to catch the response.

$_GET['data'] = 'woot'; // don't hard code this line, just an example!
ob_start();
require_once 'rest.php';
$contents = ob_get_contents();
ob_end_clean();
header ('Content-Type: application/json');
echo json_encode($content);
exit;

Give that a try, you might need to tweak it a bit, just typing this from memory.

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
  • I'll be using it for external servers as well, this is for testing right now. Thank you for the code, i tried it and it didn't seem to work. lost all page css formatting and the javascripts on the page are rendering the code text instead of processing it – Viking NM Apr 28 '17 at 12:57
  • CSS? I thought this was a JSON endpoint you were trying to access? – delboy1978uk Apr 28 '17 at 13:01
  • yes butputting that in the calling php to get the data from rest.php messed up the page's formatting and script tags and the $content is null – Viking NM Apr 28 '17 at 14:14
  • found why the formatting was getting messed up but the $content is still null – Viking NM Apr 28 '17 at 14:19
  • Can you edit your question and show the PHP code that you are calling? Also, I meant to put ob_end_clean() (I'll edit the snippet), although I don't think that would suddenly make it work :-P – delboy1978uk Apr 28 '17 at 14:20
  • thank you, updated with the rest.php file. but still the same result – Viking NM Apr 28 '17 at 16:36