3

stupid question I think.

I have an API that i want to access. if I simply put the url in my browser it returns all the results correctly.

https://api.mydomain.com/v1/name?user=user1&pass=1234

in my browser this returns array information:

{"firstname":"John","Surname":"Smith"}    

I want to be able to use PHP to simply assign the results of the URL page to a variable:

$url="https://api.mydomain.com/v1/name?user=user1&pass=1234";
$result=parse($url);
print_r($result);

This obviously doesnt work but just looking for the correct syntax. done some research but not getting any luck. should be so simple but is not.

advice appreciated as always. Thanks

4
  • 1
    the function should be parse_url(). Commented Aug 18, 2015 at 11:04
  • Do you have a custom function called parse()? Because "parse()" isn't a core PHP function. php.net/manual-lookup.php?pattern=parse&scope=quickref - parse_url() is a core PHP function php.net/manual/en/function.parse-url.php if that's what you meant to use. Commented Aug 18, 2015 at 11:11
  • voted to close as unclear. @ me when you let us all know, then I'll remove the vote. Commented Aug 18, 2015 at 11:23
  • Hello. Its not that stupit bro... I have the same question. :| Commented Sep 11, 2021 at 20:01

4 Answers 4

6

Just make a request to your API service:

$url="https://api.mydomain.com/v1/name?user=user1&pass=1234";
$result = file_get_contents($url);

Then, if I understand correctly, your API returns JSON response, so you have to decode it:

$vars = json_decode($result, true);

Which will return an array with all the variables:

echo $vars['firstname']; //"John";
echo $vars['Surname']; //"Smith";
Sign up to request clarification or add additional context in comments.

1 Comment

What if the result is not in json format? It only outputs for example a float number?
1

solution: file_get_contents (or maybe you'll need curl if ini_get("allow_url_fopen") !=1 ....)

$url="https://api.mydomain.com/v1/name?user=user1&pass=1234";
$result=parse(file_get_contents($url));
print_r($result);

hope your "parse()" function knows how to parse the result from your api call. i guess you don't know what you're doing, and next you'll be asking why the parse function is not defined :p (it looks like you're looking for json_decode , just a guess.)

i think your parse function would look like:

function parse($apiresponse){return json_decode($apiresponse,true);}

Comments

0

If you have the CURL library installed, you could do this:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'https://api.mydomain.com/v1/name?user=user1&pass=1234'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$vars = json_decode($result, true);
//do something useful with the $vars variable

Comments

-1

Cannot you do some search?
Take a look at Google with keywords "PHP get results from URL"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.