Displaying information from JSON request - php

For the sake of having you guys being able to read what I'm doing, I will post this with the api key and then edit it after.
How do I display the information from this url?
https://www.googleapis.com/youtube/v3/videos?part=snippet&id=UItqDZuHOsM&key=AIzaSyAQ26GN-removedapikey
I am using this code, but I don't understand the correct way to grab the json stuff. I've tried tons of different ways.
$vidkey = $vid_row['youtube_id'];
$apikey = "removed";
$json_output = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$vidkey&key=$apikey");
$json = json_decode($json_output, true);
//video title
$you_title = $json['snippet']['title'];
I guess I don't understand the hierarchy of it all.

Use Below code to get Title :
$vidkey = $vid_row['youtube_id'];
$apikey = "**********";
$json_output = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$vidkey&key=$apikey");
$json = json_decode($json_output, true);
echo $json['items'][0]['snippet']['title'];

Try do to a print_r($json) to understand what you're exactly doing. But just navigate in it as an object. In the current case just do:
$json['items'][0]['snippet']['title'];
But if you want to do it with a more easy way to do it (object way), just remove the true from your json_decode function
$json->items[0]->snippet->title;

Related

Store json response in a variable in php

I am trying to send json request and store the response in a variable using php using the below code
$hmaps_request = "https://geocoder.api.here.com/6.2/geocode.json?app_id=xxx&app_code=yyy&searchtext=3891 Delwood Drive, Powell, OH, United States";
$json = file_get_contents($hmaps_request);
$details = json_decode($json, TRUE);
I could not get anyerrors as well as any response. But if i paste the url in browser i could get json response.
Detects Your Problem
If you read the $http_response_header array (generated by file_get_contents() call), you'd find out that you got a "400 Bad Request" response and thus got nothing. So something must have gone wrong with your URL.
After a brief inspection, I think perhaps its your "searchtext" parameter that's blocking you. For starter, valid URL query don't usually have spaces in it. Probably why the API server says that you're a "Bad Request".
Normally, you need to properly escape the URL query string to have a proper URL. Modern browsers are very good at translating it for you seamlessly and automatically so you might not have this problem by using the URL directly in browsers.
Solution
Let's put this theory to a test. We'll use http_build_query() to escape your query query parameters:
(For privacy reason, the parameters are modified. Please substitute the query variables)
$query = http_build_query([
'app_id' => 'your-app-id',
'app_code' => 'your-app-code',
'searchtext' => '123 Some Address',
]);
$url = 'https://geocoder.api.here.com/6.2/geocode.json?' . $query;
$json = file_get_contents($url);
$details = json_decode($json, TRUE);
var_dump($details);
I seem to get proper decoded response now. Try it yourself.
There is an error in your request URL change the request URL
From
$hmaps_request = "https://geocoder.api.here.com/6.2/geocode.json?app_id=NT2iR8TD1kAeCxERIow8&app_code=-r9pZGDuz6G5NWToLaCSUQ&searchtext=3891 Delwood Drive, Powell, OH, United States";
To
$hmaps_request = "https://geocoder.api.here.com/6.2/geocode.json?app_id=NT2iR8TD1kAeCxERIow8&app_code=-r9pZGDuz6G5NWToLaCSUQ&searchtext=3891+Delwood+Drive+Powell+OH+United+States";
Here is the full code
$hmaps_request = "https://geocoder.api.here.com/6.2/geocode.json?app_id=NT2iR8TD1kAeCxERIow8&app_code=-r9pZGDuz6G5NWToLaCSUQ&searchtext=3891+Delwood+Drive+Powell+OH+United+States";
$json = file_get_contents($hmaps_request);
$details = json_decode($json, TRUE);
print_r($details);
I think the error in your request URL. Please check below code.
$url = "https://geocoder.api.here.com/6.2/geocode.json?app_id=NT2iR8TD1kAeCxERIow8&app_code=-r9pZGDuz6G5NWToLaCSUQ&searchtext=3891 Delwood Drive, Powell, OH, United States";
$url = str_replace(" ","%20",$url);
$json = #file_get_contents($url);
$details = json_decode($json, TRUE);
print_r($details);

file_get_contents with Variable

Hey so I'm having a slight dilemma with getting the contents of a file using a variable.
So to explain the code below a little, the respform fetches JSON array all ok. And the results url when echo'd displays like a normal URL that when viewed displays JSON data. Then I want to fetch JSON data from the second URL. If I use this variable in file_get_contents nothing happens. If I simply create a variable $url = '' and type the same address it works fine.
I've var dumped the $resulturl variable that I'm using and it is a string(56). I've tried using json_encode and it becomes a string(64). What sort of data does it need to be to be accepted into the file_get_contents.
$resp = file_get_contents($url, FALSE, $context);
$respform = json_decode($resp, TRUE);
$resulturl = $respform['resultsUrl'];
$data = file_get_contents($resulturl, FALSE);
$insta_array = json_decode($data, TRUE);
print_r($insta_array);
Hope someone can help, Thanks!
$resulturl apparently contains a JSON-encoded URL. You need to do:
$resulturl = json_decode($respform['resultsUrl']);

Cannot Parse Paypal "notify_url" Data Into an Associative Array (PHP)

I am setting up a Paypal sandbox. Everything works fine. When the transaction is done, paypal POSTS some data to my "notify_url" page (which I've named process-payment.php).
Now, when I post:
$array = $_POST;
$encodedString = json_decode($array);
Now, I can PUT that encoded string in the database, and it looks like:
{"mc_gross":"10.00","protection_eligibility":"Eligible",
"address_status":"confirmed","payer_id"}
Now, my big question is, how can I get THAT (^^^) into an associative array, where I can store those values in a database that records the transaction? Thank you so much for your help in advance! I've already tried:
$pp_array = file_get_contents('php://input');
$arrayDump = json_encode($pp_array);
$pp_array = json_decode($pp_array, true);
Which, obviously, didn't work. So, kinda hoping someone can give me a little tutelage here!
You are trying to use json_decode on an bad array because your payer_id is NULL. Consider filling or removing it. This is a working example with a filled payer_id:
$json = '{"mc_gross":"10.00","protection_eligibility":"Eligible", "address_status":"confirmed","payer_id":"2"}';
$json_asoc = (json_decode($json, true));
print $json_asoc['mc_gross']; // 10
For details see here
So, turns out that the code I used to make it work was this:
$array = $_POST;
$arrayDump = json_encode($array);
file_put_contents('payment-record.txt', $arrayDump);
$fileContents = file_get_contents('payment-record.txt');
$pp_array = json_decode($fileContents, true);
That gave me a workable array. Not sure why I had to write it first, but there you go.

Parsing Instagram json data to usable variable in php

I have a working api call to get basic user info and I am trying to return just the "followed_by" portion of the json data from "counts".
This link should return just the json page on your browser
https://api.instagram.com/v1/users/self/?access_token=3514554632.a691e29.3f773f5f335a4ba98fc9609d1d405fb0
And here is my code to try and parse the result, but all I am getting is a blank screen.
$jsondata = file_get_contents("https://api.instagram.com/v1/users/self/?access_token=3514554632.a691e29.3f773f5f335a4ba98fc9609d1d405fb0");
$json = json_decode($jsondata, true);
$json2 = json_decode($jsondata);
//method one to parse, not working
$parsedvalue = $json['counts'][1]['followed_by'];
echo $parsedvalue;
//method two to parse, not working
$followercount = $json2->counts->followed_by;
echo $followercount;
Here is the fix I got figured out for //method 2
$jsondata = file_get_contents("https://api.instagram.com/v1/users/self/?access_token=3514554632.a691e29.3f773f5f335a4ba98fc9609d1d405fb0");
$json2 = json_decode($jsondata);
$followercount = $json2->data->counts->followed_by;
This properly returns your follower count, but I still need help understanding how to fix method one

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