Decode G2A search results (json) to array - php

Help me to grab name of games from url
This page output json format.
I try to convert it to array, but my code not work. Please help me!
$url = 'https://search.g2a.com/items/select?json.wrf=jQuery111003403934023808688_1411464896728&q=NOT+type%3Aindividual+AND+(-type%3Agaming+AND+wholesaleQty%3A%5B1+TO+*%5D+AND+wholesaleMinPrice%3A%5B0+TO+198%5D)&wt=json&start=0&rows=10000&sort=sortOrder+DESC&_=1411464896757';
$content = file_get_contents($url);
$json = json_decode($content, true);
echo "<pre>";
print_r($json);
echo "</pre>";

You should remove the JSONP parameter json.wrf from the URL first:
https://search.g2a.com/items/select?q=NOT+type%3Aindividual+AND+(-type%3Agaming+AND+wholesaleQty%3A%5B1+TO+*%5D+AND+wholesaleMinPrice%3A%5B0+TO+198%5D)&wt=json&start=0&rows=10000&sort=sortOrder+DESC&_=1411464896757
This will return a proper JSON result.

The output from that URL (that I get currently starts with):
jQuery111003403934023808688_1411464896728({"responseHeader" ...
This isn't a pure JSON response, but rather a JSONP response.
If you're just trying to parse it in PHP, maybe something like:
$url = ...; // Your URL Here
$data = file_get_contents($url);
$pos = strpos($data, '{');
$data = substr($data, $pos, strlen($data) - $pos - 2);
$json = json_decode($data, true);
echo "<pre>";
print_r($json);
echo "</pre>";

Related

Format 15-digit number PHP

I'm trying to format the following number from a json url.
1835488750000000
to
18,354,887
Here is my code
$json_url = $Api;
$json = file_get_contents($json_url);
$data = json_decode($json, true);
$total = $data['value'];
echo number_format(trim($total , 0));
It returns
1.83549E+15
I'm tying to format the value of totalbc from the api url
Thank you
You could divide it by 1E8 and use number_format:
number_format(1835488750000000/1E8);

How to Json In PHP 'iyas'

http://simsimi.com/getRealtimeReq?uuid=VbQqwsQ3qiCw1T04VrrFQzBBlkfZibg3YFkbWz6VquZ&lc=id&ft=1&reqText=Eh+jelek&status=W
How to get value from respSentence
Im using php lang ^_^
" {"status":200,"respSentence" 11 \:"respSentence"}"` is not valid json. Use some online sites to validate your json and try like this,
$json = '{"status":200,"respSentence" :"iyas"}';
$array = json_decode($json, true);
echo $array ['respSentence'];
You can try using file_get_contents and json_decode PHP functions
$url = "http://simsimi.com/........";
$data = file_get_contents($url);
$json = json_decode($data, true);
echo $json ['respSentence'];
hope it helps

Can't print a simple JSON?

I know it's basic, but I couldn't get what's wrong because it's not my area.
I am sending some request to a server and print the response like this:
$rest = curl_init();
curl_setopt($rest,CURLOPT_URL,$url);
curl_setopt($rest,CURLOPT_GET,1);
curl_setopt($rest,CURLOPT_HTTPHEADER,$headers);
curl_setopt($rest,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($rest,CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($rest);
$json = json_decode($response, true);
echo $response;
Where I get this:
{"results":[{"Devices":["52401E7E-C5D7","AE80C0F8-999E","764BFD92-9753","78A23379-2C14","EEA03545-5EB9","E18DDFEC-C3C9"],"UserID":"433011AC-228A-4931-8700-4D050FA18FC1","createdAt":"2015-11-04T15:06:33.564Z","objectId":"3os7BGxRoG","updatedAt":"2015-11-04T17:08:57.635Z"}]}
Then, I am trying to print the JSON or its fields, but I then get nothing:
echo $json;
echo $json['UserID'];
echo $json['Devices'];
To clarify the comments a bit:
$str = '{"results":[{"Devices":["52401E7E-C5D7","AE80C0F8-999E","764BFD92-9753","78A23379-2C14","EEA03545-5EB9","E18DDFEC-C3C9"],"UserID":"433011AC-228A-4931-8700-4D050FA18FC1","createdAt":"2015-11-04T15:06:33.564Z","objectId":"3os7BGxRoG","updatedAt":"2015-11-04T17:08:57.635Z"}]}';
$json = json_decode($str, true); // to an associative array
// to echo the UserID
echo "userID : " . $json['results'][0]['UserID'];
// output: userID : 433011AC-228A-4931-8700-4D050FA18FC1
// to get the structure of the json array in general use print_r() as AbraCadaver pointed out
print_r($json);
In your attempt you were missing the results[0] part.

Parse Data from JSON URL

I'm trying to get data onto my website by parsing data from a JSON format. The URL is http://api.bfhstats.com/api/onlinePlayers and I'm trying to output for example the currently players online on PC.
Here's the current code I have:
<?$json = file_get_contents("http://api.bfhstats.com/api/onlinePlayers");
$data = json_decode($jsondata, true);
echo $data->pc->peak24;?>
I thought this would work, however it's not displaying anything. I am very new to parsing JSON data so if someone could explain what I'm doing wrong that would be brilliant.
change:
$data = json_decode($jsondata, true);
to
$data = json_decode($json, true);
Also, json_decode returns an array so use:
echo $data['pc']['peak24'];
to access the data.
You first call the variable $json but then use $jsondata in json_decode.
You are missing the foreach cycle to fetch the two dimensional array $data:
<?php
$json = file_get_contents("http://api.bfhstats.com/api/onlinePlayers");
$data=array();
$data = json_decode($json, true);
//print_r ($data);
foreach ($data as $pc) {
echo $pc["peak24"]."<br>";
}
?>
Check the $json and $jsondata that have different name but should be the same.

How to output json data in array using curl

I have code here that will get json data using curl. Now I want to echo all data. This will show no output.
<?php
$json = file_get_contents('myurl');
$data = json_decode($json);
print_r($data);
?>
This is json coming from my url:
({"Response_Code":"0000","ResultMobilePrefix":["0917","0905","0906","0915","0916","0926","0927","0937","0935","0817","0936","0922","0923",
"0932","0933","0934","0942","0943","0907","0908","0909","0910","0912","0918","0919","0920","
0921","0928","0929","0930","0938","0939","0948","0949","0925","0989","0999","0947","0998","
0946","0975","0977"]});
All you need is
echo json_encode($json);
Use true to convert it into array;
$data = json_decode($json,true);
echo '<pre>';
print_r($data);
for displaying json in format use json_encode() and for proper format use JSON_PRETTY_PRINT
header('Content-type: application/json');
echo json_encode($json,JSON_PRETTY_PRINT);
you need to get rid of the bracket and semicolon on the JSON file first before you can use the json_decode();
<?php
$json = file_get_contents('myurl');
//remove the brackets
$json = str_replace("(", "", $json);
$json = str_replace(")", "", $json);
//remove the semicolon
$json = str_replace(";", "", $json);
$data = json_decode($json);
print_r($data);
?>
this is a bit ugly, but hope you get the idea, you need to remove that character first
I just want to add to what Manish has explained above. This is what PHP Docs say about the second parameter:
When TRUE, returned objects will be converted into associative arrays.
else you will simply get a stdclass object
The problem is the URL does not return a valid Json
you could simply try
var_dump($data);
this will return null because it's not a valid json see json_decode
this will work
$json = file_get_contents('myurl');
$json = preg_replace('/[ ]{2,}|[\t\n\r\(\)\;]/', '', trim($json));
$data = json_decode($json);
print_r($data);

Categories