Can't read a string of JSON - php

I have this JSON (is from Steam Market):
{"success":true,"lowest_price":"$5.79","volume":"2,932","median_price":"$5.79"}
And I have this code:
$urlm = 'http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name='.$name;
$market = json_decode(file_get_contents($urlm), true);
echo $market['lowest_price'];
But when I try it, they give me an error:
Notice: Undefined index: lowest_price
What is wrong? Because I have an other JSON with the same method and works perfect.

You should urlencode your varaiable:
$urlm = 'http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name='. urlencode($name);

Related

How to get the view count of a YT video with PHP?

I have the following code:
<?php
$id = 'bsTY5cTi3nI';
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" . $id . "&key=...");
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{"entry"}->{'yt$statistics'}->{'viewCount'};
echo $views;
?>
I want to get the number of views of a YT video with PHP. But I get the following error message:
Notice: Undefined property: stdClass::$entry in D:\XAMPP\htdocs\Project1\WatchToGether\test2.php on line 5
Notice: Trying to get property 'yt$statistics' of non-object in D:\XAMPP\htdocs\Project1\WatchToGether\test2.php on line 5
Notice: Trying to get property 'viewCount' of non-object in D:\XAMPP\htdocs\Project1\WatchToGether\test2.php on line 5
Could someone please help me with my problem. I would appreciate answers.
You're looking for the statistics object from the video resource. You can get it like so:
<?php
$id = 'bsTY5cTi3nI';
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=" . $id . "&key=...");
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->items[0]->statistics->viewCount;
echo $views; // 264447

get value from json file via php non array

I'm a beginner in json please help
I'm trying to access value of certain objects from an online published json file via php script and not able to do so following the examples from this forum
<?php
$str = file_get_contents('http://data.companieshouse.gov.uk/doc/company/02050399.json');
$json = json_decode($str, true);
$companyname = $json["primary topic"]["CompanyName"];
print $companyname;
?>
i get the following error
( ! ) Notice: Undefined index: primary topic in C:\wamp\www\json.php on line 4
Call Stack
# Time Memory Function Location
1 0.0000 244456 {main}( ) ..\json.php:0
I have tried single and double quotes, [0] for array but to no avail
You should have to use primaryTopic :
$str = file_get_contents('http://data.companieshouse.gov.uk/doc/company/02050399.json');
$json = json_decode($str, true);
$companyname = $json["primaryTopic"]["CompanyName"];
print $companyname;
Output will : ZENITH PRINT (UK) LIMITED
I think you had a mistake at 'primary topic' key. The key-name i saw in the response is 'primaryTopic'. Could you please check again?

php json_decode not working on json array

I have been searching the internet for hours now, and i can't figure it out.
<?php
$json = '{"pages":[{"name": "Page1","inputs":[{"title": "Catagory","name": "catagory","type": "radio","options":[{"name": "Paper","value": "paper"}{"name": "Letter","value": "letter"}]}{"title": "Title","name": "title","type": "text"}{"title": "File","name": "file","type": "file","fileName": "?pages[0].inputs[0]"}{"title": "Submit","type": "submit"}]}]}';
$result = json_decode($json, true);
var_dump($result);
echo $result['pages'][0]['name'];
echo $pages[0]['name'];
?>
Im just simply trying to parse some json but the website says this:
NULL
Notice: Undefined variable: pages in C:\Users\hazzj\Desktop\Stuff\Apache-Server\htdocs\WMS\Author\submit\test.php on line 7
You'd missed out commas between strings {}. Use this modified $json variable:
$json = '{"pages":[{"name": "Page1","inputs":[{"title": "Catagory","name": "catagory","type": "radio","options":[{"name": "Paper","value": "paper"},{"name": "Letter","value": "letter"}]},{"title": "Title","name": "title","type": "text"},{"title": "File","name": "file","type": "file","fileName": "?pages[0].inputs[0]"},{"title": "Submit","type": "submit"}]}]}';
$result = json_decode($json, true);
echo $result['pages'][0]['name']; // Output: Page1
echo $pages[0]['name']; // Not sure what this $pages variable is

Read out a json file php

Hi I have this json code
{"points":[ {"10":"10"}, {"1":"1"} ]}
and this is my php code
$pointsfirst = $row['points'];
$points = json_decode($pointsfirst,true);
$getit = $points['points'][1]['10'];
echo $getit;
$row['points'] is from my database where I have stored the json
and I keep getting this error
Notice: Undefined offset: 10 in
/Applications/MAMP/htdocs/projectg/getpointsapi.php on line 46
what am I doing wrong ??
Right way is:
$getit = $points['points'][0][10];
echo $getit;
Try
$getit = $points['points']['1']['10'];
with the quotas! instead...
Arrays in PHP start with offset 0, not 1. This means you should probably use $getit = $points['points'][0]['10']; instead.

Opencart Session Variable Errors

I'm having trouble sending data via sessions because I'm getting errors of undefined variable while defining the variable in controller/checkout/shipping_address.php under validate() function. (checkout/shipping_address/validate).
$this->session->data['ship_date'] = $this->request->post['ship_date']; //<- line 102
In controller/checkout/shipping_method
$ship_date = $this->session->data['ship_date'];
if(empty($ship_date)) echo "var empty";
$ship_date = explode("-", $ship_date);
$ship_date = $ship_date[0] . "/" . $ship_date[1] . "/" . $ship_date[2];
and then I do
$quote = $this->{'model_shipping_' . $result['code']}->getQuote($shipping_address, $ship_date);
Also yes, in model/shipping/fedex.php I allow usage of $ship_date parameter. Yet after that I get.
Invalid JSON: Notice: Undefined index: ship_date in
/var/www/catalog/controller/checkout/shipping_address.php on
line 102[] parsererror Notice: Undefined index:
ship_date in
/var/www/catalog/controller/checkout/shipping_address.php on
line 102[]
You should debug the arrays $this->session->data and $this->request->post.
The reason you see those errors is that there is no index ship_date in $this->session->data and in $this->request->post. So you get a Notice: Undefined index:.
Because of the notices that are printed your afterwards outputted json becomes invalid.
Actually, OpenCart only talks through JSON. So adding this will help.
$JSONarray = array("date" => $this->request->post['ship_date']);
$this->session->data['ship_date'] = json_encode($JSONarray);
When you want to use it,
$JSONarray = $this->session->data['ship_date'];
$arr = json_decode($JSONarray, TRUE);
$Value = $arr['ship_date'];
We have to make the data JSON and then send it

Categories