How to use ipinfo.io's json for php - php

How can we use this json for php?
$ curl ipinfo.io
{
"ip": "178.78.135.10",
"hostname": "No Hostname",
"city": null,
"region": null,
"country": "AM",
"loc": "40.0000,45.0000",
"org": "AS49363 Orange Armenia CJSC"
}
I've found this at here : http://ipinfo.io/

Use file_get_contents to fetch the data and json_decode to turn it into php.
$data = json_decode(file_get_contents("http://ipinfo.io/"));
print_r($data); /* print results */

Using this server-side (i.e. PHP) will display your server's IP and information, not the user's.
If you want information on the user, you need to use JavaScript to implement their JSONP API to have the user's browser call the API and return info on the user's IP.
Alternatively, you need to call their API for the user's IP:
$data = json_decode(file_get_contents("http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}/json"));

Related

Does Facebook Graph API Post Picture url (safe_image.php) expire?

I'm grabbing Facebook Post's pictures via Graph API call:
GET /mypageid/posts?fields=id,created_time,picture
Here's a sample response:
{
"data": [
{
"id": "1389378987988776_1745791089014229",
"created_time": "2016-08-03T21:55:39+0000",
"picture": "https://external.xx.fbcdn.net/safe_image.php?d=AQBQL02u4kJtLNFs&w=130&h=130&url=fbstaging%3A%2F%2Fgraph.facebook.com%2Fstaging_resources%2FMDExNzQ1NzkwNjg5MDE0MjY5Ojk5ODEyOTY4Ng%3D%3D&cfs=1"
},
{
"id": "1389378987988776_1745301615729843",
"created_time": "2016-08-02T18:10:53+0000",
"picture": "https://external.xx.fbcdn.net/safe_image.php?d=AQD7O17OAki29Gus&w=130&h=130&url=http%3A%2F%2Fcdn2.obsnocookie.com%2Fwp-content%2Fuploads%2F2015%2F03%2F13995025_770x433_acf_cropped.jpg&cfs=1"
}
}
Does the picture URL have an expiration date?
There is no specific expiration date afaik, but you should never rely on CDN links being available later according to Facebook. If you need them, you can download and store them on your own server.

Which parameters are needed to use GET verify_credentials Twitter API method?

i just need some help with Twitters' API
I used the sign in with twitter flow to get the tokens needed to make calls to the API, there weren't any problems with the implementation, and so far I have got both oauth_token and oauth_token_secret so I think I have what I need to use the API.
The problem is about the last method which the documentation said about verif credentials, I am supposed to use that GET method to get a json with information about the user which has just logged into twitter by my app, however, Twitter docu doesn't mention anything about what parameters I need to pass or what headers, just the url.
I used php curl to make the calls to the API, do you know what parameters I need to pass to the API to finally get the json?
Here you have the link to the API docu:
https://dev.twitter.com/rest/reference/get/account/verify_credentials
Thanks in advance.
No params, just the 'Authorization' header.
Use the "OAuth Signature Generator" on the link you included to generate a test request and confirm yourself.
You just need to properly sign the request. This provides the context including the user, since it implicitly includes details about your request, the client secret, the user token etc.
$ oksocial https://api.twitter.com/1.1/account/verify_credentials.json
{
"id": 999999,
"id_str": "999999",
"name": "Bobby Bonson",
"screen_name": "xxxx",
"location": "CA",
"description": "...",
"url": null,
"entities": {
"description": {
"urls": []
}
},
"protected": false,
"followers_count": 699,
"friends_count": 631,
"listed_count": 34,

JSON and file_get_contents

i have really a big problem, i have created a login API, and i'm facing a problem, when i access my website from URL it gives me the Code :
{
"status": 200,
"user_data": {
"username": "",
"email": "",
"first_name": "",
"last_name": "",
"gender": "",
"avatar": "",
"cover": ""
}
}
That's mean i have successfully received the data, but when i'm trying to get the data via file_get_contents(); it don't work, and start to give me php errors from the sender server:
{
"errors": {
"error_id": 2,
"error": "User not logged in"
}
}
and that's mean that the user is not logged in, but i'm logged in when i access the URL directly,
the question is how can i generate a JSON data to send it and receive it from another server or domain like Facebook graph, or soundcloud API, they both are working with file_get_contents();
How should your PHP script know, that you are logged in with your browser? Please take a look at oauth2. You have to implement the possibility to authorize your script to get access to the userdata.

retrieve linkedin profile url from php api

I am using the Simple LinkedIn PHP Library to create a searching feature for an application. The user can search through LinkedIn and retrieve matched users information. As of right now the feature is working properly, but how can I parse the XML returned by the API and get the user's ID to create a link to their profile?
This is a sample of the array returned by the API call:
{
"numResults": 17325,
"people": {
"_count": 10,
"_start": 0,
"_total": 110,
"values": [
{
"firstName": "Leon",
"headline": "Digital Marketing | Mobile Marketing | Video Marketing | SEO/SEM | Digital Media Sales Trainer",
"id": "EFMHqoTad3",
"lastName": "E. Spencer",
"pictureUrl": "http://m3.licdn.com/mpr/mprx/0_jOgiHrinvW_afCa6PRDyHPXZBdvfi3a6xyWyHPXke7T0p5JQl4SfQ1KEMXzr86fopjxjFqnEf1I-"
},
If I understand correctly, a users profile url looks like http://www.linkedin.com/profile/view?id=156996610 so I should just be able to extract the ID from the API call and append to the view?id= query right? Can I do this with the SimpleXML PHP Library?
By XML I believe you mean JSON.
One way in php is by using the json_decode() function.
$url = file_get_contents("http://www.linkedin.com/profile/view?id=156996610");
$json =json_decode($url,true);
the second value "true" means it will be returned as an associative array.
Then you can access whatever you want from that response.
I.E a user ID..
$userID = $json['people']['values']['id'];
I suggest reading the json_decode() documentation to understand how this works.

Inconsistency from Graph API

I'm currently writing a PHP application.
I noticed something strange when it wasn't returning the persons birthday.
To cut a long story short, when i manually query the graph api, it gives me the full result set (birthday, email etc. included). However, when I run it in my app, it comes back as if i've not sent the access token.
When I manually visit the address it produces:
{
"id": "507665705",
"name": "Ally Dewar",
"first_name": "Ally",
"last_name": "Dewar",
"link": "https://www.facebook.com/alastair.dewar",
"username": "alastair.dewar",
"birthday": "06/22/1990",
"location": {
"id": "113744028635772",
"name": "Greenock"
},
"gender": "male",
"email": "alastair\u0040alastairdewar.co.uk",
"timezone": 1,
"locale": "en_GB",
"verified": true,
"updated_time": "2012-03-11T15:56:19+0000"
}
However, when my application makes the same request (exact same URL), it comes back with
{"id":"507665705","name":"Ally Dewar","first_name":"Ally","last_name":"Dewar","link":"http:\/\/www.facebook.com\/alastair.dewar","username":"alastair.dewar","gender":"male","email":"alastair\u0040alastairdewar.co.uk","locale":"en_GB"}
My instinct says it could be a setting within the facebook app, as it wasn't a problem beforehand.
EDIT: https://developers.facebook.com/bugs/412680422092698?browse=search_4f854dc037a2a5d12811839
Thanks for any advice.
Make sure you ask for all the permissions that you need and also remove the app and start again. Make sure you ddon't cache any of the responses as well.
How do you query the API? Graph Explorer or just manually open the link (graph.facebook.com/USER_ID) and pass the auth token generated by the app? Testing with the Graph Explorer won't help you with your problem as permissions etc are different.
As mentioned by Nobita, code helps. This is as much as I can advise considering the amount of data you're giving us.

Categories