Where can I see what print_r display on PHP? - 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.

Related

Blank results on json parse on https api calling using php and MAMP

I´m trying a very simple php script which is about calling a json data through api calling on a online https link using MAMP.
However if I use the following code I have blank results:
<?php
$cnmkt = "https://api.coinmarketcap.com/v1/ticker/?limit=50";
$json = file_get_contents($cnmkt);
$fgc = json_decode($json,true);
echo $fgc[1]['percent_change_7d'];
?>
But if i copy/paste the content of the https link into a test.json file locally, substituting the https link with the test.json file on $cnmkt variable, the same exact script works properly.
I know i´m missing something very obvious, if someone could help me that would be very much appreciated, thanks.
Stefano
The script is working fine. I get an expected result of 4.63
Disable your AV/firewall and check again.

Can't get content with file_get_contents function

I want to get movie Rating from IMDB website.
For example, I have a url http://p.media-imdb.com/static-content/documents/v1/title/tt1520211/ratings%3Fjsonp=imdb.rating.run:imdb.api.title.ratings/data.json
Run it and you can see the movie rating is 8.7 and I wan to get it, so I used this query:
$s="http://p.media-imdb.com/static-content/documents/v1/title/tt1520211/ratings%3Fjsonp=imdb.rating.run:imdb.api.title.ratings/data.json";
$s = file_get_contents($s);
$s=explode('rating":',$s);
$s=explode(',"rating',$s[1]);
echo $s[0];
But it doesnt work and I don't know why!
I am checking the script out but uhm ye your array contains nothing check it with: print_r($s)
the output is:
Array ( [0] => )
updated
When you show the content it shows:
‹5KOÃ0„ÿJµ'šÄvÞ9U‚.‰³›LÁ¢y`o*UUþ;v ·õÎìÌg3ôÇØj6ãgl—ñáF‡¬©¹Ñ4#ÓH µ†ÏxÛlŽödñ³ÀñKï¥4Óe*€èˆ2¤DÕŠH×U‘zÔ"‡?q°Ó¡5^5i\gÕ’Ü´Ø¡ðÀ×Ùd“žMÌ¡õŸ.ԚЗlÛ„YæJ()/l«€ù…݇>{ÿKí_0_Þa BÔÚR£„{êôè¿æ lx¤­š*.ï§iÙC—…Rb]1“$æ6
so your explode will also be checked to that string.
Code:
$s="http://p.media-imdb.com/static-content/documents/v1/title/tt1520211/ratings%3Fjsonp=imdb.rating.run:imdb.api.title.ratings/data.json";
$s = file_get_contents($s);
/*$s=explode('"rating":',$b);
$s=explode('"rating":',$b[1]);
print_r($s);*/
print($s);
updated:
also check the code with JSONLint
it seems like it has an error
I think you can find more in this question for your answer:
json to php array using file get contents
I assume the PHP.ini setting allow_url_fopen is disabled, which means that remote URIs (ie http(s), etc) can not be opened using the file family functions (fopen,..)
Please also refer to its config value description according to which it is a system wide setting, so I think you can not override it using ini_set().
Alternatively you could use the cURL extension, for details on how to use it please refer to its documentation.
Please run php info to check if the allow_url_fopen is set to ON
php_info();
But use the CURL for getting the content, which simulating an actual web client.
Do not turn on this option, because it's not a good for security purposes

Get and decode json with php

I'm sure this is similar to other questions, but I have tried the solutions, and couldn't get it to work.
I'm trying to get the location of an IP address, decode the json, and print it. I actually plan to put it into a database, but Im not even sure why I cant get anything to print to the screen.
$ip = $_SERVER['REMOTE_ADDR'];
$json = file_get_contents("http://api.hostip.info/get_json.php?ip=".$ip);
$local = json_decode($json,true);
thanks.
A. When you mean I cant get anything to print to the screen. definitely you can get anything because you did not output anything to screen.
error_reporting(E_ALL);
$ip = $_SERVER['REMOTE_ADDR'];
$json = file_get_contents("http://api.hostip.info/get_json.php?ip=".$ip);
$local = json_decode($json,true);
echo $local['city'] ;
print $local['country_name'] ;
var_dump($local);
B. It would also mean that allow_url_include set to Off which would not allow file_get_contents load a information from another domain
Firstly, the code you've provided does not actually print anything, which would explain why you're getting a blank screen. I assume you're doing something like echo $local; later in the program, but if not, that'll be the problem.
Assuming that isn't the issue....
Two possibilities:
file_get_contents() is returning an error.
json_decode() is returning an error.
Firstly, you should add some additional error trapping in your code to determine which one is the problem.
If you have PHP set to not display errors, consider switching them on so you can see any errors that are generated. Or else look at your server log to see the errors.
file_get_contents() may fail if the URL is invalid. It may also fail if your PHP installation is set to not allow URLs via the file handling functions. If this is the case you'll need to change your php.ini settings. If you can't do that, you'll have to load the file via a different method (eg Curl).
json_decode() is only likely to fail if the string it gets is invalid JSON. You should check the output of the URL to confirm that it looks right, and check it something like firebug to ensure it is valid.
Hope that helps.

Include through HTTP

I have a file on another server which I can trust since it's my own code that in it there is an array with some settings.
When I include it using the include_once and print_r the variable in it, I am getting an undefined variable.
I also tried to return the variable from the file I am including and assign it to a variable in the script like this:
$var = include($url);
where $url has:
$array = array(1,2,3); return $array;
when I print_r($var) I only get 1.
It's because the other server will parse the file and return the RESULT, not the actual code. you need to get the actual contents of the file, which will only be possible if you disable php on the other server or ftp to it.
I would recommend copying the file over to the server you're working on, safer an easier
The file you are trying to get will come back the same as if you went there in your web browser because the remote web server will parse the contents of the file though the php engine.
remote host:
echo serialize($array);
local host:
$array = unserialize(file_get_contents($url));

Ajax issues, Invalid JSON

I'am building simple Ajax application (via jquery). I have strange issue. I found where the problem is, but I don't know how to solve it.
This is simple server-side php code:
<?php
require('some.php');
$return['pageContent'] = 'test';
echo(json_encode($return));
?>
On the client side, the error "Invalid JSON" is thrown.
I have discovered that if I delete require function, everything work fine.
Just for information, the "some.php" is an empty php file. There is no error when I open direct php files.
So, conclusion: I cant use require or include function if I want to use ajax?
Use Firebug to see what you're actually getting back during the AJAX call. My guess is that there's a PHP error somewhere, so you're getting more than just JSON back from the call (Firebug will show you that). As for your conclusion: using include/require by itself has absolutely no effect on the AJAX call (assuming there are no errors).
Try changing:
<?php
require('some.php');
$return['pageContent'] = 'test';
echo(json_encode($return));
?>
To:
<?php
$return = array(
'pageContent' => 'test'
);
echo json_encode($return);
?>
The problem might have to do with $return not being declared as an array prior to use.
Edit: Alright, so that might not be the problem at all. But! What might be happening is you might be echoing out something in the response. For example, if you have an error echoing out prior to the JSON, you'd be unable to parse it on the client.
if the "some.php" is an empty php file, why do you require it at all?!
require function throws a fatal error if it could't require the file. try using include function instead and see what happens, if it works then you probably have a problem with require 'some.php';
A require call won't have any effect. You need to check your returned output in Firebug. If using Chrome there is a plugin called Simple REST Client. https://chrome.google.com/extensions/detail/fhjcajmcbmldlhcimfajhfbgofnpcjmb through which you can quickly query for stuff.
Also, it's always good to send back proper HTTP headers with your response showing the response type.
It's most likely the BOM as has been discussed above. I had the same problem multiple times and used Fiddler to check the file in hex and noticed an extra 3 bytes that didn't exist in a prior backup and in new files I created. Somehow my original files were getting modified by Textpad (both in Windows). Although when I created them in Notepad++ I was fine.
So make sure that you have your encoding and codepages set up properly when you create, edit, and save your files in addition to keeping that consistent across OSes if you're developing on windows let's say and publishing to a linux environment at your hosting provider.

Categories