2

If you access the webpage https://api.mercadolibre.com/items/MLB752465575 you will receive a JSON response. All I need to start is print the item "id" on the screen.

This is my code:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
  </head>
  <body>
  <?php

    $json_str = "https://api.mercadolibre.com/items/MLB752465575";

    $obj = json_decode($json_str);

    echo "id: $obj->id<br>"; 

  ?>
  </body>
</html>

All I want is receive the MLB752465575 part into my browser.

How can I do it?

0

3 Answers 3

3
$json_str = "https://api.mercadolibre.com/items/MLB752465575";

The above does not retrieve the data it's saving the url to the var and that's not what you want.

You just need to fetch the content You can use cURL or file_get_contents()

cURL version:

<?php

$url = "https://api.mercadolibre.com/items/MLB752465575";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$r = curl_exec($curl);
curl_close($curl);
$array = json_decode($r, true);

echo "<pre>";
print_r($array);
echo "</pre>";

?>

file_get_contents version:

<?php
$r = file_get_contents('https://api.mercadolibre.com/items/MLB752465575');

echo "<pre>";
echo print_r(json_decode($r, true));
echo "</pre>";
?>

Both of them will work unless the remote website requires you to be human (has extra verifications to stop robot requests). cURL would be a better way if that were the case because you can fake a user agent using a header array.

Once you have the array build it's just a matter of accessing the required data. using $r as an array result of the remote json structure.

Sign up to request clarification or add additional context in comments.

4 Comments

Not true, the URL actually links to the raw JSON when using PHP, tried myself within a PHP script.
Your right it works when used in the code. just tested it. Weird that if you open the website it's all formatted. Interesting concept hehe
They're simply showing HTML when specifically requested in the headers, which is what our browsers do, otherwise they send raw JSON :)
Yeah that's what I figured but I still find it interesting hehe I might use that my self on one of my projects lol
1

Use curl to get the result, and json_decode to turn it into an array.

<?php
$url = "https://api.mercadolibre.com/items/MLB752465575";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpcode != 200) {
      echo "error " . $httpcode;
      curl_close($ch);
      return -1;
}
$result_arr = json_decode($result, true);
echo $result_arr['id'];
curl_close($ch);

Comments

0
$jsonResponse = file_get_contents('https://api.mercadolibre.com/items/MLB752465575');
$obj = json_decode($jsonResponse);

echo "id: {$obj->id}<br>";

What you did in your code was to json_decode the URL itself. You needed to get the content from the URL, and then decode the content.

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.