JSON Decode PHP (Curl vs File_Get) - php

Currently, I am trying to use the Twitch API to grab Twitch stats such as current viewers, Title, and more I am running into an issue with #File_Get_Contents when using this my request seem to be delayed or not getting them as quick as I refresh, i.e. I think the results may be cache'd.
For example here is my old code
$twitch = json_decode(curl_get_file_contents('https://api.twitch.tv/kraken/channels/'.$twitch_channel), true);
$display_name = $twitch['display_name'];
$game = $twitch['game'];
$status = $twitch['status'];
$url = $twitch['url'];
$avatar = $twitch['logo'];
$views = $twitch['views'];
$followers = $twitch['followers'];
The main issue with this is it didnt seem like it updated every time I refreshed, so I looked into using cURL for better results + I heard it's much quicker with load time!
Here is my current curl code
$requesturl='https://api.twitch.tv/kraken/channels/' . $twitch_username;
$ch=curl_init($requesturl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$cexecute=curl_exec($ch);
curl_close($ch);
$twitch = json_decode($cexecute,true);
$display_name = $twitch['display_name'];
$game = $twitch['game'];
$status = $twitch['status'];
$url = $twitch['url'];
$avatar = $twitch['logo'];
$views = $twitch['views'];
$followers = $twitch['followers'];

Your log is showing PHP Notice, You don't have any error. I change a lil bit your code to test it and It's working. So you're probably just no printing your vars.
Check your code with a print_r online

So ultimnately I figured out what the issue was, it's XAMP Curl not working for whatever reason. It's setup properly in the PHP.ini, I have the Two .DLL in my Sys32. So I dont know why this isn't working

Related

Trying to get property of non-object geolocation google API

I'm working in to get the latitude and longitude from google maps through the geolocation API. I have tried many times but always is showing this error:
Trying to get property of non-object in C:\wamp64\www\index3.php on line 16
The issue is that I'm not able to get the info from the $url so then, cannot get the latitude and longitude.
I changed my code and now it is, but still with no working.
<php
$address = urlencode('BTM 2nd Stage,Bengaluru,Karnataka,560076');
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&key=*************************************";
$json = file_get_contents($url); // I tried with the # symbol and without it
$data=json_decode($json);
echo ($data->results[0]->geometry->location->lat);
?>
And also I used this one:
<php
$address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$encode = urlencode($address);
$key = "insert key here";
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$encode}&key={$key}";
$json = file_get_contents($url); // I tried with the # symbol and without it
$data = json_decode($json);
echo "Latitud".($data->results[0]->geometry->location->lat);
?>
Without the # symbol I have this error line
Warning: file_get_contents() has been disabled for security reasons in [...][...] on line 8
change http
to https in your request url.
As Abrucius, you msut use HTTPS or google will deny the request. You must also encode the address or else you will get a "Bad request" error.
<?php
$address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$encode = urlencode($address);
$key = "insert key here";
$url = "https://maps.googleapis.com/maps/api/geocode/json?address={$encode}&key={$key}";
$json = file_get_contents($url);
$data = json_decode($json);
echo ($data->results[0]->geometry->location->lat);
Outputs: 12.9082396
replace following code
$address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&key=AIzaSyDjWI3J9CNlANoB5PXNKq3nz1ZbYeOrNGE";
to
$address = urlencode('BTM 2nd Stage,Bengaluru,Karnataka,560076');
$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$address&key=AIzaSyDjWI3J9CNlANoB5PXNKq3nz1ZbYeOrNGE";
You have provided http in the url, it should be https. Try below code that will help you. You have to use urlencode() because api will not allow space in the address, so you have to encode.
$address = 'BTM 2nd Stage,Bengaluru,Karnataka,560076';
$address = urlencode($address);
$url = "https://maps.google.com/maps/api/geocode/json?address={$address}&key=***************************";
$resp_json = file_get_contents($url);
$resp = json_decode($resp_json, true);
echo "<pre>";print_r($resp["results"][0]['geometry']['location']['lat']);
Try this code will work for sure
WillParky93's answer seems to work fine for me. Maybe try following the next link to allow https wrappers:
How to get file_get_contents() to work with HTTPS?
...or post the error you're getting so we can help you out
I just solved this issue, I was getting this error because of a proxy configuration. I was testing into a network with a proxy configuration. So, the proxy is not allowing me to connect with the Google database. Then I tried to connect with a different network and it worked perfectly. Thank you guys for your aid!

Get a JSON from url with PHP

Im new to php and tried to get a json object from the twitch API to retrieve one of its values and output it. i.e
i need to get the information from this link: https://api.twitch.tv/kraken/users/USERNAME/follows/channels/CHANNELSNAME
plus i need to to something so i can modify the urls USERNAME and CHANNELSUSERNAME. I want it to be a api to call for howlong user XY is following channelXY and this will be called using nightbots $customapi function.
the date i need from the json is "created_at"
Since we were able to clear out the errorsheres the final PHP file that works if anyone encounters similiar errors:
<?php
$url = "https://api.twitch.tv/kraken/users/" . $_GET['username'] . "/follows/channels/" . $_GET['channel'];
$result = file_get_contents($url);
$result = json_decode($result, true);
echo $result["created_at"];
?>
You have a typo in your code on the first line and you're not storing the result of your json_decode anywhere.
<?php
$url = "https://api.twitch.tv/kraken/users/" . $_GET['username'] . "/follows/channels/" . $_GET['channel'];
$result = file_get_contents($url);
$result = json_decode($result, true);
echo $result["created_at"];
You have to call the page this way page.php?username=yeroise&channel=ceratia in order to output the created_at value for this user and this channel.
In your code you're using 2 different ways to get the content of the page and you only need one (either file_get_contents or using CURL), I chose file_get_contents here as the other method adds complexity for no reason in this case.

Google + button counts showing "0" using the Sharrre library ( Json , Php )

So checked via a phpinfo() and Safe Mode on my server is off, Curl is activated and there are no reasons for it not to work.
I also made sure Sharrre.php is in my root directory. Even included the Curlurl to the php file. Tried both absolute and relative linking. The google button with the counter shows as soon it is uploaded but not as expected because the counter shows 0 the entire time.
The culprit seems to be: $json = array('url'=>'','count'=>0);
After a few lines of other code we got this:
if(filter_var($_GET['url'], FILTER_VALIDATE_URL)){
if($type == 'googlePlus'){ //source http://www.helmutgranda.com/2011/11/01/get-a-url-google-count-via-php/
$contents = parse('https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQurl=' . $url . '&count=true');
preg_match( '/window\.__SSR = {c: ([\d]+)/', $contents, $matches );
if(isset($matches[0])){
$json['count'] = (int)str_replace('window.__SSR = {c: ', '', $matches[0]);
}
}
So either the google url code is not valid anymore or... well maybe there is something wrong with the suspected culprit because:
when changed to a value higher than 0 $json = array('url'=>'','count'=>15);
It shows 15 counts as you can see. I want it to be dynamic though and get the counts I already have and update those per click.
What can be done to solve this?
In my particular case the problem was in the asignement of the URL to the Curl object.
The original script sharrre.php sets the URL by asigning it to an array element of the curl object, but this is not working and causes Google counter not retrieve any amount.
Instead, the URL must be asigned by the curl_setopt() function.
This resolved this problem in my case:
sharrre.php:
//...
$ch = curl_init();
//$options[CURLOPT_URL] = $encUrl; // <<<--- not working! comment this line.
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_URL, $encUrl ); // <<<--- Yeeaa, working! Add this line.
//...
Hope this help.

Steam url api inserting variables

I have been attempting to use steam api to display a persons name. When I insert there steamid in directly it works however, when I try to use a variable it doesn't work. The thing that confuses me is that I have this exact same code for my queue that works however this does not work. I have looked around and I believe I am doing this right but for some reason it is not working. Any help here would be great. Thanks.
<?
$name = 76xxxxxxxxxxx;
$response = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/? key=xxx&steamids={$name}&format=json";
$middle = (file_get_contents($response));
$json = json_decode($middle, true);
$b = $json['response']['players'][0]['personaname'];
print_r($b);
?>
Your $name variable needs to be wrapped in quotes.
$name = "76xxxxxxxxxxx";
This will properly create your response variable

A weird result, as a result of this PHP script

require_once 'include/BestBuy/Service/Remix.php';
$skuid = rawurldecode($_GET['skuid']);
$apiKey = 'tfvs7h89pwn4pzmyj9nxemmg'; // Your API key
$remix = new BestBuy_Service_Remix($apiKey);
$result = $remix->product('$skuid','json')
->show(array('url'))
->query();
$data = json_decode ($result, true);
$feed = $data['url'];
print <<< FEEDS
$feed
FEEDS;
When I put this script into my page, the $feed will echo the current URL.
But when I manually supply the script with an integer, replacing ($skuid) it will be successful.
It's really weird, But I think it has something to do with me using a variable in that specific array.
And it is also weird, because It was working before I re arranged some of the HTML.
I'm trying to approach this problem the most logical way.
Please help.
Thanks.
should you have $skuid in quotes? I would expect:
$result = $remix->product($skuid,'json')
rather than
$result = $remix->product('$skuid','json')

Categories