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

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!
prileyosborneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

invsman249Commented:
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

0
prileyosborneAuthor 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!
0
invsman249Commented:
you could try get or removing that option all together.
Also you might want to try

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

prileyosborneAuthor Commented:
Thanks again, does it matter at what point of the xcript I use it in?
0
invsman249Commented:
i dont think so, i would put it above the CURLOPT_USERPWD options though
0
prileyosborneAuthor 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.
0
invsman249Commented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prileyosborneAuthor 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.
0
prileyosborneAuthor 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!
0
invsman249Commented:
ah nice one glad i could help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.