Fetch JSON object with API link by PHP - php

I'm struggling to get JSON object with API link by PHP. (http://hots.adspreemedia.com/api/characters). This link should allow me to fetch a list of characters as JSON object.
What I tried was the following code.
<?php
$list = file_get_contents('http://hots.adspreemedia.com/api/characters');
echo $list;
?>
But it just shows only {"status":"200","message":"OK","data":[1,2,3,4]} in my localhost. My final goal is to create a list of characters in PHP file.

Follow the link you posted. it matches exactly what you are getting back.
To get more data, you will need to follow their api docs for retrieving more info from them
Is this a site you are building? there is no sigup form, and no documentation I can find without logging in to help you with

If you want to fetch [1,2,3,4] then do following
$list=json_decode($list,true);
$chars=$list['data']; //-- The array of characters

Related

Weird output when trying to list all customers in Stripe using php

I created a database customers.txt where are saved all my created customers in Stripe. Now I want to list all the customers. This is my code in php for listing customers.
$e= \Stripe\Customer::all(array(
'limit' => 3
));
echo $e;
}
But the output is weird:
IMAGE:
Can someone help me to list the customers?
Now I have got my JSON and run this:
$e=\Stripe\Customer::all(array(
"limit"=>10
));
$customers=json_decode($e,true);
var_dump($customers);
I get just NULL response!
That output is not wyrd, it's a Stripe JSON string. standing for Javascript Object Notation (website).
There's a lot of questions on Stackoverflow about JSON so ask things such as How to convert JSON string into a PHP array.
Also Stripes own documentation (which is very good), states:
JSON is returned by all API responses, including errors, although our API libraries convert responses to appropriate language-specific objects.
from the Stripe Documentation
Edit:
You can read A useful question about turning a JSON string into an object and vice versa
So now you know what JSON is
Using it to get a PHP customer object. (revised)
$e // customer JSON of all customers.
$customers = $e->__toArray(true);
//$customers = json_decode($e);
And then process the array $customers as you need to in your applicaton.
NOTE:
The value of $customers or $customersArray will be an Object or a String data type so you need to treat the appropriately, and they will not display with echo because echo is a string output function, so you need to use print_r() or var_dump() to display these values -in their raw form- on the screen.
EDIT TWO
Recommended that from your screenshot that you format the API response from Stripe into an Array of Objects. This can be done by following this Stack Overflow Answer here.
Please review my revised code above.

Get fan count for lots of facebook pages

I have lots of facebook pages (we're a club) and would like to display them in order of their fan count. I'm new to php and know nothing about API. All the code I can find to return the fan count refers to apps, and needs their IDs and secrets, which I don't have with a business page.
I have this:
http://api.facebook.com/method/fql.query?format=json&query=select+fan_count+from+page+where+page_id%3D355692061120689
When I put this into a browser it gives me what I need. How do I write this in php so it gives me a variable to work with?
Thanks.
This data is called a JSON string. There is a function in php called json_decode(). Using this in tandem with file_get_contents(), we can retrieve the value, and echo it in php:
<?php
$json = file_get_contents('http://api.facebook.com/method/fql.query?format=json&query=select+fan_count+from+page+where+page_id%3D355692061120689');
$decode = json_decode($json);
echo $decode[0]->fan_count;
?>
To be clear, $decode is an array, whose first value is a php object, whose variable fan_count holds the data.
In regards to the graph url, just change..
echo $decode[0]->fan_count;
to..
echo $decode->likes;
of course assuming you've changed the file_get_contents() url to that of your graph url.

Assigning JSON Value to PHP Array from Google Maps API Query

I am trying to find a way to assign the JSON value Google Map's API Returns when passed an address. I would search Google, but I am only a few month deep into PHP and really don't know what keywords to use. I really don't even know what a JSON is?
Anyway, I found this article that explains how to query Google Maps with an address and have it return a GPS Location. When I tested it in my browser, the text appears. I just don't know what to use to take what the browser is displaying and assign in to an Object in a PHP page.
This is the Google Maps Query:
http://maps.google.com/maps/geo?q="ADDRESS"&output=csv&oe=utf8
This my function where I want to use this:
//Queries Google Map's API with an address and assigns the returned GPS Location
//to this Object
public function build_me($this_address)
{
//Builds the query from the address array to send to the Goole Maps API
$query = $this_address["Line1"].",".$this_address["Line2"].",".
$this_address["City"].",".$this_address["State"].",".$this_address["Zip"];
//Location_Array = http://maps.google.com/maps/geo?q=$query&output=csv&oe=utf8
$this->latitude = //Location_Array['Something'];
$this->longitude = //Location_Array['Something'];
}
This is the article that I am referring to:
http://martinsikora.com/how-to-get-gps-coordinates-from-an-address
My question is, how would I go about doing this? Are there any good tutorials on this?
Thanks in advance for anyone pointing me in the right direction!
Actually the URL you are showing will produce a CSV output (that is why it has the output=csv parameter). You can see it if you simply insert it into your browser (Downing Street 10):
200,8,45.9797693,-66.5854067
Here are some useful resources to get the job done:
cURL to make the call to the Google API
cURL Tutorial
str_getcsv() to parse a CSV string into a PHP array
(json_decode() to translate JSON to PHP arrays/objects - just in case you're gonna need it later)

Using API to get Covers - URL Error

I need some album covers for my PHP website but something is not working anymore.
I was using a JSON to get Album ID and then i was using this id to get the album cover.
My problem is that i try to decode a json from website but i don't get any result anymore (it was working before ...).
Here is the code (getting JSON and decoding it) :
$req="http://api-v3.deezer.com/1.0/search/track/?q=".$deezer."&index=0&nb_items=8&output=json";
$result = file_get_contents($req);
$testjson=json_decode($result,true);
I think you are using an old code of Deezer api (1.0 and I don't get any JSON from your url).
Try to change $req by :
$req="http://api.deezer.com/2.0/search/album/?q".$deezer."&index=0&nb_items=8&output=json";
Then, some code are missing and are important to change too.
When you try to get your image, don't forget to change the url too with that version (2.0).
You can find more information on Deezer Api : here.

How to fetch youtube video data using php?

I need to get the XML data from youtube for a particular video(based on ID) and fetch the thumbnail url. I am using PHP.
The code is as follows:
<?php
//$xmlFilePath = 'http://localhost/testFile.xml'; //This works..
$xmlFilePath = 'https://gdata.youtube.com/feeds/api/videos/5P6UU6m3cqk?v=2';
$dataFromYoutube = file_get_contents($xmlFilePath);
print_r($dataFromYoutube);
?>
I dont get any data. The output is blank. If I load the url 'https://gdata.youtube.com/feeds/api/videos/5P6UU6m3cqk?v=2' in browser and copy the content to a file(http://localhost/testFile.xml), then the above code works.
I have tried fetching the youtube data using ajax and also simlexml_load_file function in php. But the result is blank/empty for Youtube url. However, I get the data for 'http://localhost/testFile.xml' in both cases.
How can I fetch youtube data using php?
Please help.
well, your code is correct and works fine too. It may be that your apache server is not configured for "https". So just check it once too.

Categories