Pinterest API - pure json response? - php

So I made a simple HTTP request to Pinterest's API to get the count of a link:
$this->load->library('rest');
$this->rest->initialize(array('server' => 'http://api.pinterest.com/'));
$return_data = $this->rest->get('v1/urls/count.json?callback=&url=' . $link);
The response I get is:
receiveCount({"count": 5743, "url": "http://google.com"})
You can try this yourself here.
I don't want the callback and I tried setting callback= but the parenthesis are still present so I can't parse it via json_decode.
Is there a better way of getting a pure json response without having to string replace the parenthesis myself?

The Pintrest API is currently in development and is not ready for public use; they removed their own documentation a while ago. There is however a cache on Bolt for v2 of the API.
For now I would just do a regex on the response such as
$return_data = preg_replace('/^receiveCount\((.*)\)$/', "\\1", $return_data);
And then json_decode that.

Related

PHP Get REST Api and parse JSON

I have done research and I can't seem to find a solution. I have a website running XenForo and added XenAPI to the Software.
When you call the API: ( api.php?action=authenticate&username=USERNAME&password=PASSWORD)
it returns the following JSON:
{
"hash": "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
}
I am trying to find the "proper" way of capturing the JSON and decoding it. I hope you guys can help! Note that I am looking for a example to learn from, because all other examples fail.
For simple requests with 1-2 params, you can use
$json = file_get_contents(url...);
$response = json_decode($json, true);
For more difficult queries use curl extension.
For full web-applications use Guzzle - curl wrapper for communicates with API.

How to send request to Graph Api and fetch events data?

i am beginner to graph api/php . i want to get events data against page_id etc.when i paste url into browser it returns json data but when i use curl or file_get_contents(); method it returns "Null". What is the proper way to sending request and getting response. this is my basic code and other than this i am NOT using any kind of include files etc.
$json_link = "https://graph.facebook.com/getwellgabby/events/attending/?fields=id,name,description,timezone,start_time,cover&access_token=206516302813755|lRnrKAiu3Z......&since=1356998400&until=1483228800";
file_get_contents($json_link);
$obj = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
var_dump($json);
RESULT: boolean (false)
https://developers.facebook.com/docs/graph-api/reference/v2.3/event/attending
As you can see in the docs, the correct way to get attendees for an event is to use the Event ID:
https://graph.facebook.com/{event-id}/attending?access_token=xxx
You canĀ“t get all attendees for all events in one API call, you have to do this event by event. Test it in the API explorer before using any PHP code.
Btw, you should use CURL instead of file_get_contents. The PHP SDK uses CURL too.

PHP cURL response returning a string, how do I handle and select from the data?

I'm using a Codeigniter curl library to do a simple get request to a url. When I echo the response, it's coming back as a string. I can't quite figure out what I can do to convert that string into a useable format, because I need to be selecting from specific parts of the return to progress in my function.
My code is ran through Codeigniter, using Philsturgeon's curl library, here's a simple example of what I'm using:
$this->load->library('Curl');
$response = $this->curl->simple_get('https://api.twitch.tv/kraken/channels/username');
var_dump($response);
I feel like I'm missing something simple here. Any help?

PHP HTTP request to get JSON response

I am a novice in PHP. I have a URL and I need generate a GET request to this URL and get a JSON response. How might I achieve this?
You can perform GET requests using the following. . . provided PHP is not in safe mode.
file_get_contents();
curl();
You can use http://php.net/curl library to send GET requests.

Need help understanding Google Maps API URL?

i am kind of new to API thing, and i need help understanding on what exactly is happening with the below Code.
$address = 'Bhatkal, Karnataka, India';
$requestUrl = 'http://maps.google.com/maps/geo?output=xml&key=aabbcc&oe=utf-8&q='.urlencode($address);
$xml = simplexml_load_file($requestUrl);
i understand that HTTP is capable of sending Request and getting response in return isn't it? what i am unable to understand is the third and last function that is $xml = simplexml_load_file($requestUrl); when i do a print_r($xml) i get an object in return which prints all the object details i got back as response,
how does the function process the
URL?
does it use CURL (i have very less idea about what is CURL).
and where do i look up for Google Maps API URL?
That function does not process the request (nor the URL), only the response, Google processes the URL that, the function just "visit's" it. You can do as well: here. The XML file you see here is ending up in the variable $xml, parsed.
EDIT: the URL in this post is not working too well, because of the key parameter
simplexml_load_file internally uses the fopen wrapper and opens the remote xml that would be produced by the url and then converts into an array for php to easily use.
The response object will help you to extract the data from the response.
Check out the details of Google Maps API

Categories