[PHP JSON] Is there a way to grab remote JSON data with PHP

prileyosborne
prileyosborne used Ask the Experts™
on
I know that there is a way to do this with JavaScript, but I need to be able to access the JSON object with PHP only as well. Is there a way to do this. My JSON data is on the same server, though in a different port, and it has a username and password.

I just need to grab the whole object and then I can split it and use it.

Let me know if you need any code samples, but what i need is:

1. Grab JSON object from my server
2. Enter username and password so i can grab it

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
i haven't tested this but maybe something like this

 
<?php

$json_url = "http://www.someurl.com/data.json";
$user = "your_username";
$pass = "your_password";

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
curl_setopt($ch, CURLOPT_URL, $json_url);

$output = curl_exec($ch);      
curl_close($ch);

echo $output;

var_dump(json_decode($output));
?>

Open in new window

Author

Commented:
Ok, I tried that with the first three variables updated with my inforamtion and I get a result of Null.  When I go to the actual url I see the data is there, so it shouldn't be null

Not sure if there is anything taht you can see that would change it.  The url I am using has a port extension that could be changing things.  So it ends with :1010 instead of just the ip address. Also, I know that the javascript that I was using was using GET to retrieve the data.  Should I maybe change the CURLOPT_POST to CURLOPT_GET?

Thanks so much for all your help!
you could try get or removing that option all together.
Also you might want to try

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Author

Commented:
Thanks again, does it matter at what point of the xcript I use it in?
i dont think so, i would put it above the CURLOPT_USERPWD options though

Author

Commented:
So I added that and it doesn't seem to work, My current script is:

$json_url = "http://11.11.111.11:1010/query";
	$user = "user";
	$pass = "pass";

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
	curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
	curl_setopt($ch, CURLOPT_URL, $json_url);

	$output = curl_exec($ch);      
	curl_close($ch);

	echo $output;

	var_dump(json_decode($output));

Open in new window


So very close to waht you supplied. Let me know if there is something else wrong.  I am hoping it is not something wrong with the server.
does the script give you any feedback as to what is wrong?
Is there any documentation as to how the user and password should be supplied?

you could switching the httpaut try using this which will try any authentication method it can
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

if that doesn't work possibly you need to send the user and pass as some kind of argument like this



 
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $json_url);
$data = array(
    'user' => $user, 
    'pass' => $pass
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$output = curl_exec($ch);      
curl_close($ch);
echo $output;

var_dump(json_decode($output));

Open in new window

Author

Commented:
Ok, the output I get for this is 'null', so I am assuming it is iconnecting and not getting anything.  Do you know of a simple error script I could run that would show why it is null?  Like does it time out or does it get to the page and receive no data. Or is it not even making the connection. It is loading this script for a long time, so I think that might be the issue.

Author

Commented:
Ok, so I got it to work!  It turns out there was an issue on my server and by changing the URL in the variables to localhost from the IP fixed it and this script worked perfectly. Thanks so much!
ah nice one glad i could help!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial