Facebook JSON Decoding Events in PHP - php

I have:
$user = json_decode(file_get_contents(
'https://graph.facebook.com/me?access_token=' .
$cookie['access_token']), true);
var_dump($user);
which works fine and gives profile output.
But:
$events = json_decode(file_get_contents(
'https://graph.facebook.com/me/events?access_token=' .
$cookie['access_token']), true);
var_dump($events);
gives:
object(stdClass)#5 (1) { ["data"]=> array(0) { } }
I'm not sure if this is an empty object, or if I'm not accessing what's inside 'data' correctly. But, I know there are in fact events associated with my profile. Permissions have been granted, so that's not the problem. Anyone know how to return all event names for my profile?

In order to get events in PHP you have to follow these steps,
Request a proper Access Token follow this link
facebook: permanent Page Access Token?
Replace your Access token below texted YOUR_ACCESS_TOKEN field
<?php
$json_string = 'https://graph.facebook.com/5973249561/events/?access_token=YOUR_ACCESS_TOKEN&fields=id,name,description,start_time,place,cover,end_time&limit=999';
$obj = json_decode(curl_get_contents($json_string),true);
foreach ($obj['data'] as $key => $value) {
echo #$value['id'];
echo #$value['name'];
echo #$value['start_time'];
echo #$value['place'];
echo #$value['cover']['source'];
}
function curl_get_contents($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>

Related

How can I access the property of an object in PHP from the result of a separate PHP file?

I'll try to keep this short and to the point. I'm setting up an API which requires using POST to generate an 'authorization token', which I can then use in combination with GET to retrieve data. The results are in JSON format, of course. I have an auth.php file which works and gives the token, among other object properties. My question is how do I use the token in a separate GET.php file to retrieve the data I need?
Is there a way to access Object properties from the end result of my auth.php file?
Here is the code which isn't working in my GET.php file:
if (isset($_POST['TOKEN'])) {
$api_key = '11111111111111111111111111111';
$utoken = $_POST['TOKEN'];
$url = 'https://api.rmaster.com/api/daltonwholesale/catalogattributes?company='.$company.'&catseq='.$catseq.'&api_key='.$api_key;
$company = '99';
$catseq = '98838';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($ch);
if (!$result) {die("Connection Failure");}
curl_close($ch);
echo $result;
} else {
echo $_POST['TOKEN'];
}
?>```

Unable to get json data from rest api in php

code:
<?php
$url = base_url()."eventapi";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));
?>
eventapi controller
<?php
require APPPATH . '/libraries/REST_Controller.php';
use Restserver\Libraries\REST_Controller;
class Eventapi extends REST_Controller
{
function __construct()
{
parent::__construct();
$this->load->database();
}
function index_get()
{
$data['client_id'] = $this->session->userdata('client_id');
$client_id = $data['client_id'][0]['client_id'];
$this->db->select('*');
$this->db->from('event');
$this->db->where('client_id',$client_id);
$sql = $this->db->get();
$result = $sql->result_array();
$this->response($result, 200);
}
}
output:
array(0) { }
eventapi data
[{"id":"1","client_id":"20190702082406","event_type":"Private Event","event_name":"Birthday Celebration","event_date":"2019-08-15","event_time":"08:00 PM","event_des":"Birthday celebration ","s_date":"2019"}]
I am creating a simple rest API using Codeigniter where API works fine. Now, the problem is that when I am getting data from eventapi using curl it shows me output i.e array(0) { } I don't know why? So, How can I solve this issue? Please help me.
Thank You
If the above one is your code, you didn't add any parameters. The array passing with empty input. So you get empty output. Please correct the input.
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
$inputParameters");
Please add these lines along with the curl .
And don't forget to add the header also .
//set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
//return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

I want to validate user login using api access_token in php

I am having this demo link to get acces_token for the user verifications of the user login.
http://testapp.test/app.php/v1/verifyUser?username=admin&password=password&grant_type=password&client_id=6_20jeacfmdv0gwgg4kcgw44swwkoo8ogog8g4gwggwo0wko08ow&client_secret=38311bdlv884wc0ws4g048k0ieei8oc4oc0cmswok8w33o0s9e
After sending request using postman i am getting the access token in the response
<?xml version="1.0"?>
<user_info>
<access_token>NGI20QFmNDE1HAUjYWEyMjVlbsryNWRjNGJmOGI5ZDNiLhA1OTI5MjkzZjIwZmE4YmYxOHYwMTQ3NTUzYTdmMg</access_token>
<expires_in>86400</expires_in>
<token_type>bearer</token_type>
<scope/>
<refresh_token>AWMxYmVhYmFkNDJkOTVjYjM1YWQwNTVmOTcwYmIyKaR98TE1Y2E5KLZkZTY0ZjA3Mjc3MzQzOeGmTTUzODlmYw</refresh_token>
<user_id>007</user_id>
<username>admin</username>
<email>email#admin.com</email>
<firstname>admin</firstname>
<lastname>admin</lastname>
<ip_address>xxx.xxx.xx.xxx</ip_address>
<last_login>
<date>2019-07-12 18:21:27.000000</date>
<timezone_type>4</timezone_type>
<timezone>Central</timezone>
</last_login>
</user_info>
I want to use the acess_token authentication system to login the user in the website using PHP.
Here is the solution to my problem
$url = 'http://testapp.test/app.php/v1/verifyUser?username=admin&password=password&grant_type=password&client_id=6_20jeacfmdv0gwgg4kcgw44swwkoo8ogog8g4gwggwo0wko08ow&client_secret=38311bdlv884wc0ws4g048k0ieei8oc4oc0cmswok8w33o0s9e';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
$data = curl_exec($ch);
//echo $data;
if(curl_errno($ch))
{
print curl_error($ch);
}
else
{
curl_close($ch);
$xml = simplexml_load_string($data);
$json = json_encode($xml);
$var= json_decode($json,true);
}
if(isset($var["error"]))
{
echo "error";
}
else
{
$_SESSION['signed_in'] = true;
$_SESSION['sno'] = $var["user_id"];
$_SESSION['first_name'] = $var["firstname"];
}
Hopefully, this answer will help those who have same problem.

Github api request with php curl

I am trying to get the latest commit from github using the api, but I encounter some errors and not sure what the problem is with the curl requests. The CURLINFO_HTTP_CODE gives me 000.
What does it mean if I got 000 and why is it not getting the contents of the url?
function get_json($url){
$base = "https://api.github.com";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $base . $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($curl, CONNECTTIMEOUT, 1);
$content = curl_exec($curl);
echo $http_status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return $content;
}
echo get_json("users/$user/repos");
function get_latest_repo($user) {
// Get the json from github for the repos
$json = json_decode(get_json("users/$user/repos"),true);
print_r($json);
// Sort the array returend by pushed_at time
function compare_pushed_at($b, $a){
return strnatcmp($a['pushed_at'], $b['pushed_at']);
}
usort($json, 'compare_pushed_at');
//Now just get the latest repo
$json = $json[0];
return $json;
}
function get_commits($repo, $user){
// Get the name of the repo that we'll use in the request url
$repoName = $repo["name"];
return json_decode(get_json("repos/$user/$repoName/commits"),true);
}
I use your code and it will work if you add an user agent on curl
curl_setopt($ch, CURLOPT_USERAGENT,'YOUR_INVENTED_APP_NAME');

Get Media ID Instagram

Below is the code I am using
$decode = json_decode($json, true);
var_dump($decode);
This results in the below:
c.ak.instagram.com/hphotos-ak-xfa1/10666256_719752088073218_1127882203_a.jpg"
["full_name"]=> string(26) "Promote OLShop Harga Murah" ["bio"]=> string(0) "" ["id"]=>
string(9) "356515767" } } } }
How do I get Get Media ID?example results :
817757393383064097_356515767
Please help me.
$json = file_get_contents('https://api.instagram.com/v1/tags/gaul/media/recent?access_token=1463408808.e757b44.0738048e481448b48f1cbb23f70f0195&count=1');
$decode = json_decode($json, true);
$media_id = $decode['data'][0]['id']
If you took a look at Embedding you would find a suitable answer, for me i was in need for the media_id for a project, so i wrapped it into a function
function getMediaID($permalink) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.instagram.com/oembed?url=' . $permalink);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$oembed = curl_exec($ch);
curl_close($ch);
$oembed = json_decode($oembed, true);
return $oembed['media_id'];
}
That result you showed are when getting a USER data... not medias. To get media use some like:
https://api.instagram.com/v1/users/USERID/media/recent/?access_token=TOKEN&count=COUNT
Where:
USERID ir the user you want to get medias
TOKEN is your access token that enable you to use the API
COUNT to tell how much photos of that user you want to retrieve at this time
code by Bankzilla is working perfectly. I would upvote it , but for my reputation points are less :/. Also the code isnt echoing anything so the page will be blank.
Here is the code to display the media id. Copy it into a file with php extension for ex: getmedia.php and paste url where your are hosting it in browser and run it.
ex : www.myhost.com/mysite/getmedia.php?url="URL OF THE MEDIA HERE"
<?php
$permalink = $_GET["url"];
getMediaID($permalink) ;
function getMediaID($permalink) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.instagram.com/oembed?url=' . $permalink);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$oembed = curl_exec($ch);
curl_close($ch);
$oembed = json_decode($oembed, true);
echo $oembed['media_id'];
return $oembed['media_id'];
}
?>
You will use:
$search_response = curlRequest("get", "https://api.instagram.com/v1/users/self/media/recent/?access_token={$json_data['access_token']}");
Then:
$photo_id= $search_response['data'][$i]['id'];
echo $photo_id .'<br/>';
($i for exapmle data[1] , data[2] etc.) - (each image data)

Categories