Show PHP echo results when called by JSON rpc? - php

I am using JSON to run some PHP functions and 1 of them is not working and is returning null. How can i return a php output e.g. an echo of variable/array from the php function and view in firebug/on screen? I'm sure i have done this before but cant remember how!?
Thanks
Paul

echo json_encode(array("hello" => "world"))
Will output a JSON encoded array, you can then access this script via an Ajax call, Firebug can view this and you can debug from there?

I used this to pass POST and GET arrays into json, so:
$var = <?php json_encode($_POST); ?>
So if i have $_POST['test'] = 'what'
to access it i do $var.test which is equal to 'what'

Related

Parsing Wikipedia API with PHP

I have a PHP script that retrieves the JSON result from the Wikipedia API and stores it in $json variable, then I json decode it into $data:
<?php
$q = htmlspecialchars(($_GET["q"]));
$url = 'https://en.wikipedia.org/w/api.php?action=query&list=search&srnamespace=0&srprop=snippet&format=json&callback=json&origin=*&prop=links|extracts|categories|images&srsearch=test';
$json = file_get_contents($url);
/*
print "<pre>";print_r($json);print "</pre>";
*/
$data = json_decode($json,true);
echo $data['query']['search'][0]['title'];
This retrieves the JSON file, but I am not able to work with it. I need to extract the Title tag and echoing it like this doesn't do anything.
echo $data['query']['search'][0]['title'];
Any idea how to correct my code to retrieve the following title tag:
Remove &callback=json from your URL. That's making a request for JSONP (ironic link to wikipedia). It wraps the response with a JavaScript callback function, so instead of just JSON you need in PHP, you're getting
/**/json(THE JSON HERE)
You can see it in the page source, even if it displays as JSON on the page. Those extra characters are making json_decode fail. That parameter is intended more for cross-domain requests from JS.
It looks like you're already accessing the resulting array properly with
echo $data['query']['search'][0]['title'];
You might think it would give you some kind of warning or notice when you try to access those array keys when $data is null, but it won't.

How to use echo twice to populate data from php to html client using ajax

I am trying to use echo in php for populating data twice as per user different action using ajax.
But it seems that I am unable to use echo twice to populate data from php server to my html page.
here is the code for php:
<?php
// Receive the Data from Client
$data = $_REQUEST;
$fileName = $data['fileList'];
$files = glob( "*.txt");
//it will pass data while the html page load (and working fine)
echo json_encode($files);
//set the file path for text files
$file_Content = file_get_contents("temp.text");
//this will pass while user click a button, but while using it is not working, even the first echo also
echo json_encode($file_Content);
?>
Is there any way to do it? or any suggestion pleas..Thanks
To form and respond with a valid json object, you can only call json_encode once. You're currently responding with two encoded json objects, which is invalid, so I'm guessing only the first one is being interpreted.
Remove the first json_encode echo, and encode an array of both properties in your second (now only) echo. I've taken the liberty to guess at what might be sensical properties for your response object here:
echo json_encode(['files' => $files, 'content' => $file_Content]);

Where can I see what print_r display on PHP?

I have a REST API with Android and Slim framework. I'm using XAMPP to connect it with a MySQL database, .
I don't know where print_r is displayed.
The api.php file is in C:\Program Files\xampp\htdocs\project\app\api\api.php.
I get the following message when I try to access localhost/project/app/api/api.php:
Access denied.
It's a message that I put with define function. In index.php:
define("CONSTANT",true);
In connect.php and api.php:
if(!defined("CONSTANT")) die("Access denied");
print_r is outputted to the file whereever it is called. Just check the source of your web page and the output will be there somewhere.
If you need to find it quickly try echoing something unique above the print_r. E.g. echo "I AM OVER HERE!!!";
Let's say you've a page test.php where you're using print_r($stuff);
and I assume that you've installed xampp in C:\Program Files\xampp and your code is inside here C:\Program Files\xampp\htdocs\droidApp
droidApp is folder where this file test.php is residing.
test.php can have
<?php
print_r($stuff_you_want_to_print);
?>
go to this url from your browser
localhost/droidApp/test.php
just make sure your xampp is running before doing that.
Try this, I use this way.
echo "<pre>";
print_r($array);
echo "</pre>";
You can use Rest API extension of chrome:
https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en
In that you can hit the url of your service and can see all your print_r and var_dump values.
Finally, testing different options during some hours, I get the solution.
If you are changing three values in your PUT method of your REST API, for example, name, description and idCar, after setting their new values (that you send from your Android application) you can put on your PHP script:
print_r([$name,$description,$idCar]);
Here all it's equals than before. So, how can you see what print_r it's displaying?
After executing your response on your Android application, you can put these two lines:
HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);
And then you can put a Log to see what it's displaying:
Log.d("response",responseText);
Now you will be able to see what print_r it's displaying.
print_r prints the specified variable to the screen, similar to the way the echo command does. There's a second argument which is a boolean that is set to false by default. If you set it to true, it will allow you to store the contents in a variable rather than print it to screen.
Thus,
print_r($_GET)
is the same as
$inputs = print_r($_GET,true);
echo $inputs;
See http://php.net/manual/en/function.print-r.php for more information.

AJAX json data does not print in PHP

i have a JSON data in Javascript made form array using JSON.stringify
{
"user":"Mark",
"id":"80",
"0":["age","=","twenty four","varchar"],
"1":["prefix","=","Mr.","enum"]
}
i am sending this via AJAX to PHP file. When i echo the POST i get the values
echo (serialize($_POST['data']));
s:263:
"{
"user":"Mark",
"id":"80",
"0":["age","=","twenty four","varchar"],
"1":["prefix","=","Mr.","enum"]
}";
How can i get the POSTed data in an Array or Object. i have tried to do
var_dump(json_decode($_POST['data']));
AND
var_dump(json_decode(serialize($_POST['data'])));
AND
var_dump(json_decode($_POST['data'],true));
but they did not work. Output is null.
json_decode() should do the trick for you, but depending on your server config (if magic_quotes_gpc is on), you might need to use stripslashes() before decoding.
If your PHP versopn >=5.2.0, you can use following build in PHP functions to decode JSONS
json_decode($_POST['data'])
It return Array and StdClass object.
Edit: How did you found json_decode is not working. Please try ver_dump or print_r. Hoping your PHP version >=5.2.0
You have to store it to something.
$posted = json_decode($_POST['data']);
var_dump($posted);

JQuery, JSON, and PHP

I have been given one php file with multiple functions that are echoed. On the client end, $.getJSON has no problem processing a JSON object if only echo is allowed and the other two are commented out. However if the others are uncommented than get JSON breaks, I'm still all of the objects echo in the response. For JSON to process, does every php function need to be in a separate file? Or can JSON parse the different arrays separately from one response string? i.e
$.getJSON(samefilename,function(array 1) {
--- process here
}
$.getJSON(samefilename,function(array 2) {
}
Sorry if this a complete newbie question. I'm a bit confused on the process of parsing the response string. I would of thought it would be $_get['array1'] and $_get['array2']being echoed from php - yet if I have more than one echo in the php file my jquery breaks and shows blank in html.
Instead of:
<?php
echo $var1;
echo $var2;
do like this:
<?php
echo json_encode(Array(
'var1' => $var1,
'var2' => $var2
));
and in jQuery:
$.getJSON('file.php',function(response) {
alert(response.var1);
alert(response.var2);
}

Categories