i use json_decode function to get some data from dynamic urls.
some urls are working fine inside the decode function, others are not.
for example:
this is working fine:
$jsondata = file_get_contents("http://185.112.249.77:9999/Api/Search?search=&level=1&min=1&max=50&points=48000");
$data = json_decode($jsondata, true);
but when i change the 48000 to 45000, the decode is not working any more.
41000 is working fine again.
also.. when i add some other attributes after the points-attribute... the decode function does also not work anymore.
for example:
$jsondata = file_get_contents("http://185.112.249.77:9999/Api/Search?search=&level=1&min=1&max=50&points=20000&loc=32000007");
$data = json_decode($jsondata, true);
when i remove &loc=32000007 it works again.
when you use direct the urls in the browser, all of them are working.
only the json_decode function is not working with some of them.
is there any limitation in the characters which you can use inside the json_decode function? is there any way to handle this?
your help is much appreciated.
ok. i managed to get this problem solved.
here is one simple solution which can be adapted according to the requirements.
all characters which are listed inside the array will be replaced by character "?"
$jsondata = file_get_contents($url);
$tobecleaned = array("®");
$jsondatacleaned = str_replace($tobecleaned, "?", $jsondata);
$data = json_decode($jsondatacleaned, true);
Related
I have a URL like {"gclid":"Cj0KEQjw7dfKBRCdkKrvmfKtyeoBEiQAch0egQ97fbdyRpkv6sUmkYHXnTKlOWMywt3SceAol-1umzwaAmsk8P8HAQ","sem_keyword":"+turbolader +einbau","sem_creativeid":"165357442696","sem_matchtype":"b","sem_network":"g","sem_device":"m","sem_placement":"","sem_target":"","sem_adposition":"1t2","sem_param1":"","sem_param2":"","sem_campaignid":"629608072","sem_adgroupid":"38134265464","sem_targetid":"kwd-26982332150","sem_location":"1004645"}
from where I need to extract Cj0KEQjw7dfKBRCdkKrvmfKtyeoBEiQAch0egQ97fbdyRpkv6sUmkYHXnTKlOWMywt3SceAol-1umzwaAmsk8P8HAQ, i.e. the data between two double quotes after gclid.
I have tried all substring functions but I'm not able to get even close.
You can use MySQL JSON-functions:
select json_unquote(json_extract(url, '$.gclid'))
$json = file_get_contents('url_here');
$obj = json_decode($json);
echo $obj->gclid;
For this to work, file_get_contents requires that allow_url_fopen is enabled. This can be done at runtime by including ini_set("allow_url_fopen", 1);
I'm using Laravel-5 right now and I've just created a JSON file called test(test.json). My question is:
Where should I put my JSON file?
How can I use file_get_contents and point to that one file I wanted?
This is what I've tried which is obviously wrong:
$string = file_get_contents("../json/test.json");
$json_file = json_decode($string, true);
Thank you so much for helping!
You can store it in your storage folder in Laravel:
$path = storage_path() . "/json/${filename}.json"; // ie: /var/www/laravel/app/storage/json/filename.json
$json = json_decode(file_get_contents($path), true);
I receive the following URL
/articles?difficulty=1,2
Inside the framework I want to receive an array from that - [1,2]. Is there any method in the framework to convert params like that into an array automatically? Can the framework do that? I can do like that explode(',', $params['difficulty']) - but I'm wondering whether this can be handled by the framework.
I don't want to pass params like that:
/articles?difficulty[]=1&difficulty[]=2
There is no helper in framework Request component for converting such values, it can be easily achieved with native PHP explode function. Use:
$array = explode(',', $string);
as you suggested.
But the wrapper of explode exists - \yii\helpers\StringHelper::explode(), it has additional options for trimming and skipping empty elements, you can use it too. But most of the times using regular explode should be enough.
try this
Use the Request class.
http://www.yiiframework.com/doc-2.0/yii-web-request.html
print_r(Yii::$app->request->get()); returns all get variables in an array. It's like doing print_r($_GET); in straight php.
If you want a specific $_GET variable you access it as follows:
Yii::$app->request->get('difficulty');
In your case it would be:
$success = Yii::$app->request->get('success');
$token = Yii::$app->request->get('token');
Then after that explode it with comma, so it will get converted into array.
$array = explode(',', $success );
I have been writing part of a website I'm making, part of the stats page will display information about a websites Json response.
The address of the website is: http://steamcommunity.com/market/listings/440/Name%20Tag/render/?count=1&start=1&query=.
Here is a link to a parser so the code is easier to read http://json.parser.online.fr/.
The code I have written so far works but no matter what i try I cant get the information I need.
use JSON::XS;
use WWW::Mechanize;
use HTTP::Cookies;
use LWP::Simple;
use strict;
use warnings;
my $url = "http://steamcommunity.com/market/listings/440/Name%20Tag/render/?count=2&start=2";
my $json = get $url;
my $data = decode_json $json;
my $info = $data -> {listinginfo};
My problem is that i would like to access the price of the listing however when new listings are made available the reference for them changes. I have no idea how to deal with this and Google is not helping. Any help would be greatly appreciated, thanks in advance.
Seb Morris.
EDIT: Thanks for the replies, I have progressed my code and ended up with:
my $data = decode_json $json;
my #infoids = keys %{$data -> {listinginfo}};
foreach my $infoid (#infoids) {
my $price = $data -> {listinginfo}{$infoid}{converted_price};
print "$price" . "\n";
}
However I am getting the error: Use of uninitialized value $price in string at line 30. I dont understand why I am getting this error as I have declared the variable. Any help would be really appreciated.
If I understand, your problem is that the listinginfo object contains key(s) which change for each request, and you don't know to find out what the key is for the request you just made.
You can find the keys to a perl hash using the 'keys' function. So you can get all of the keys of the listinginfo hash like this:
my #infoids = keys %{$data -> {listinginfo}};
Note the need to use %{ } to de-reference listinfo, which is itself a hash reference.
There could be more than one info ID, although when I tested the web service you linked in your question it only ever returned one. If you are sure there will only ever be one, you can use:
my $price = $data -> {listinginfo}{$infoids[0]}{price};
Or, if there might be more than one, you can loop through them:
foreach my $infoid (#infoids) {
my $price = $data -> {listinginfo}{$infoids[0]}{price};
# Now do something with price
}
The current code takes form input and does THIS to it:
$apikey = 'myapikey';
$q = urlencode($bookSearchTerm);
$endpoint = 'https://www.googleapis.com/books/v1/volumes?q=' . $q . '&key=' . $apikey;
$session = curl_init($endpoint);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($session);
curl_close($session);
$search_results = json_decode($data);
if ($search_results === NULL) die('Error parsing json');
Just for kicks, I also did
echo $endpoint;
which shows
https://www.googleapis.com/books/v1/volumes?q=lord+of+the+rings&key=myapikey
When I enter that URL into a browser, I get a screen full o' data, telling me that, among other things, there are 814 items.
Yet when I run the code on a page, I get
Error parsing json
Can someone show me where I'm going wrong?
Thanks in advance.
By the response to the comment, it could be set, maybe not. It may also be the case that what is returned isn't parse-able by the parser because it isn't in the right data format. Check what $data gives back as a result. If it's correct then by the json_decode doc on the PHP site it may be that it's simply too big for the parser to parse (reaches recursion limit) though I doubt that.
It it is possible that the what is return just overflows the PHP allocated memory limit. PHP parses JSON into associative or numbered arrays, which can get expensive. So if what you get back is just too big, it'll fail to finish parsing and just return null.