requesting user permissions not working facebook graph api - php

hi i am using facebook graph api PHP SDK . here i am trying to get the logged in user's facebook details , currently i am getting only the basic details which are not required any permissions .
Array
(
[id] => xxxxxxxxx
[name] => Kanishka Bandara Panamaldeniya
[first_name] => Kanishka
[middle_name] => Bandara
[last_name] => Panamaldeniya
[link] => http://www.facebook.com/kanishka.panamaldeniya
[username] => kanishka.panamaldeniya
[hometown] => Array
(
[id] => 108014839219027
[name] => Kandy
)
[location] => Array
(
[id] => 108602292505393
[name] => Colombo, Sri Lanka
)
[quotes] => every action has a reaction....
[work] => Array
(
[0] => Array
(
[employer] => Array
(
[id] => 234117399957729
[name] => Cyber LMJ
)
[location] => Array
(
[id] => 108602292505393
[name] => Colombo, Sri Lanka
)
[position] => Array
(
[id] => 109542932398298
[name] => Software Engineer
)
[start_date] => 2011-08
)
[1] => Array
(
[employer] => Array
(
[id] => 235852306375
[name] => eMarketingEye
)
[position] => Array
(
[id] => 109542932398298
[name] => Software Engineer
)
[start_date] => 2010-09
[end_date] => 2011-08
)
)
[sports] => Array
(
[0] => Array
(
[id] => 103992339636529
[name] => Cricket
)
)
[favorite_teams] => Array
(
[0] => Array
(
[id] => 191485640872790
[name] => Sri Lanka Cricket Team
)
)
[favorite_athletes] => Array
(
[0] => Array
(
[id] => 185101794860886
[name] => Kumara Sangakkara
)
[1] => Array
(
[id] => 119473804782315
[name] => Angelo Mathews
)
[2] => Array
(
[id] => 164574093575397
[name] => Kumar Sangakkara
)
[3] => Array
(
[id] => 169411436417102
[name] => Virat Kohli
)
[4] => Array
(
[id] => 166273026737204
[name] => Lahiru Thirimanne
)
[5] => Array
(
[id] => 101237886616765
[name] => Dinesh Chandimal
)
)
[education] => Array
(
[0] => Array
(
[school] => Array
(
[id] => 34663330581
[name] => Dharmaraja College-Kandy
)
[year] => Array
(
[id] => 138383069535219
[name] => 2005
)
[type] => High School
)
[1] => Array
(
[school] => Array
(
[id] => 112872282058324
[name] => University of Colombo School of Computing
)
[year] => Array
(
[id] => 140617569303679
[name] => 2007
)
[type] => College
)
[2] => Array
(
[school] => Array
(
[id] => 107577089271805
[name] => University of Colombo
)
[type] => Graduate School
[with] => Array
(
[0] => Array
(
[id] => 705760115
[name] => Lasitha Benaragama
)
)
)
)
[gender] => male
[timezone] => 5.5
[locale] => en_US
[languages] => Array
(
[0] => Array
(
[id] => 104023029632921
[name] => Sinhala
)
[1] => Array
(
[id] => 106059522759137
[name] => English
)
)
[verified] => 1
[updated_time] => 2012-07-29T16:34:22+0000
)
now i want to get fields
*email
*birth day
and some other permission requesting fields .
so i am doing
$this->co_config_items = $this->ci->config->item('connections_config');
$fbconfig = array(
'appId' => $this->co_config_items['fb_appid'],
'secret' => $this->co_config_items['fb_secret'],
'cookie' => true,
'domain' => $this->ci->config->item('domain')
);
$this->ci->load->library('3rd/connectors/facebook', $fbconfig);
$facebook = new Facebook($fbconfig);
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2;
$session = $facebook->getSession();
$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'read_stream,friends_likes,user_birthday'
));
if(!$session){
$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'read_stream,friends_likes,user_birthday'
));
echo "<script type='text/javascript'>window.location = '$url';</script>";
}
else{
//echo "<script type='text/javascript'>window.opener.location.href='" . base_url() . "home/auth/get_user_facebook_details_list" . "'; self.close();
//</script>";
try {
$me = $facebook->api('/me');
$this->ci->session->set_flashdata ( 'user_details',$me);
$url=base_url()."home/auth/user_contact_information";
echo "<script type='text/javascript'>window.opener.location.href = '$url';self.close();</script>";
} catch (FacebookApiException $e) {
$this->ci->message->set_error(array('An errror occured , please try again'));
}
}
i am trying to ask permissions by
$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'scope' => 'read_stream,friends_likes,user_birthday'
)):
but it is not asking for any permission , the default array is always printing . why is that . please help . thanks in advance

Try doing this instead:
$url = $facebook->getLoginUrl(array('scope' => 'read_stream,friends_likes,user_birthday'));

i was using the version 2 of PHP sdk , now i am using version 3 , now it is working , thank you very much . i saw some changes in two versions in this function
version 2
public function getLoginUrl($params=array()) {
$currentUrl = $this->getCurrentUrl();
return $this->getUrl(
'www',
'login.php',
array_merge(array(
'api_key' => $this->getAppId(),
'cancel_url' => $currentUrl,
'display' => 'page',
'fbconnect' => 1,
'next' => $currentUrl,
'return_session' => 1,
'session_version' => 3,
'v' => '1.0',
), $params)
);
}
version 3
public function getLoginUrl($params=array()) {
$this->establishCSRFTokenState();
$currentUrl = $this->getCurrentUrl();
// if 'scope' is passed as an array, convert to comma separated list
$scopeParams = isset($params['scope']) ? $params['scope'] : null;
if ($scopeParams && is_array($scopeParams)) {
$params['scope'] = implode(',', $scopeParams);
}
return $this->getUrl(
'www',
'dialog/oauth',
array_merge(array(
'client_id' => $this->getAppId(),
'redirect_uri' => $currentUrl, // possibly overwritten
'state' => $this->state),
$params));
}
in version 3 this i can see this additional code
// if 'scope' is passed as an array, convert to comma separated list
$scopeParams = isset($params['scope']) ? $params['scope'] : null;
if ($scopeParams && is_array($scopeParams)) {
$params['scope'] = implode(',', $scopeParams);
}
in version 2 , there is nothing called scope .and also
in version 2
return $this->getUrl(
'www',
'login.php',
in version 3
return $this->getUrl(
'www',
'dialog/oauth',

Related

How to loop and get the values from second array in sequence

This is the Array I want to loop Into Events Index and get all the context print out of it how can i do this
Here is the Array Code Which i am trying to loop
[1] => stdClass Object
(
[id] => RPDNLNYO6U
[active] =>
[events] => Array
(
[0] => stdClass Object
(
[id] => RPDNLNYO6U_1
[created_at] => 2023-02-06T10:55:42.501003Z
[visibility] => all
[text] => Hi
[author_id] => 170bdd03-a163-49e9-4295-11fae376ff0e
)
[1] => stdClass Object
(
[id] => RPDNLNYO6U_2
[created_at] => 2023-02-06T10:55:44.102000Z
[visibility] => all
[text] => I need help
[author_id] => 170bdd03-a163-49e9-4295-11fae376ff0e
)
[2] => stdClass Object
(
[id] => RPDNLNYO6U_3
[created_at] => 2023-02-06T11:06:11.002000Z
[visibility] => agents
[type] => system_message
[text] => Chat is idle due to 10 minutes of inactivity
[system_message_type] => routing.idle
[text_vars] => stdClass Object
(
[duration] => 10
)
)
[3] => stdClass Object
(
[id] => RPDNLNYO6U_4
[created_at] => 2023-02-06T11:11:14.002000Z
[visibility] => all
[type] => system_message
[text] => Chat archived due to 15 minutes of inactivity
[system_message_type] => routing.archived_inactive
[text_vars] => stdClass Object
(
[duration] => 15
)
)
)
)
You can use a foreach loop in PHP to loop through the array and get all the context you need.
$array = [1 => (object) [
'id' => 'RPDNLNYO6U',
'active' => '',
'events' => [
(object) [
'id' => 'RPDNLNYO6U_1',
'created_at' => '2023-02-06T10:55:42.501003Z',
'visibility' => 'all',
'text' => 'Hi',
'author_id' => '170bdd03-a163-49e9-4295-11fae376ff0e'
]
]
]];
foreach ($array as $object) {
$id = $object->id;
$active = $object->active;
foreach ($object->events as $event) {
$event_id = $event->id;
$created_at = $event->created_at;
$visibility = $event->visibility;
$text = $event->text;
$author_id = $event->author_id;
echo "Event ID: $event_id, Created At: $created_at, Visibility: $visibility, Text: $text, Author ID: $author_id\n";
}
}
This will loop through the outer array and print out all the context for each event.

How to get list of permissions in Yii2?

I need somehow to get the list of existing permissions in my Yii2 application.
yii/rbac/DbManager as authManager is used.
And when trying to run:
$permissions = Yii::$app->authManager->getPermissions();
I receive list in which are permissions and routes:
Array ( [/admin-tools/*] => yii\rbac\Permission Object ( [type] => 2 [name] => /admin-tools/* [description] => Access admin-tools [ruleName] => [data] => [createdAt] => 1500542371 [updatedAt] => 1500542371 ) [/admin/*] => yii\rbac\Permission Object ( [type] => 2 [name] => /admin/* [description] => access admin extension [ruleName] => [data] => [createdAt] => 1500541632 [updatedAt] => 1500541632 ) [/group] => yii\rbac\Permission Object ( [type] => 2 [name] => /group [description] => [ruleName] => [data] => [createdAt] => 1502190828 [updatedAt] => 1502190828 ) ) [permission_admin] => yii\rbac\Permission Object ( [type] => 2 [name] => permission_admin [description] => [ruleName] => [data] => [createdAt] => 1500559417 [updatedAt] => 1502190927 ) [permission_hr] => yii\rbac\Permission Object ( [type] => 2 [name] => permission_hr [description] => [ruleName] => [data] => [createdAt] => 1500559479 [updatedAt] => 1500559919 ) )
I suppose there is another smarter way to get this data. Because yii2-admin module is able to get list of only permissions, but I don`t have any idea how to do this?.
The resulting
$permissions = Yii::$app->authManager->getPermissions();
is an array with all the permissions object content
if you need only name and description you can iterate over the array eg:
$permissions = Yii::$app->authManager->getPermissions();
foreach($permissions as $key => $perm){
echo 'name : ' . $perm->name . ' description : ' . $perm->description . <br />;
echo
}
you can check yii2 doc http://www.yiiframework.com/doc-2.0/yii-rbac-dbmanager.html
http://www.yiiframework.com/doc-2.0/yii-rbac-basemanager.html#getPermissions()-detail
could b you are looking for getPermissionsByUser()
http://www.yiiframework.com/doc-2.0/yii-rbac-dbmanager.html#getPermissionsByUser()-detail

insert null to database if array element is undefined using php (laravel)

i am trying to insert an array to a table but some of elements of array are not defined .
i want to insert null where element is undefined .
array is as below :
[0]=>Array
(
[0] => Array
(
[Id] => 120
[Title] => PHP
[Year] => 2011
[Version] =>5.5
)
[1] => Array
(
[Id] => 121
[Title] => Javascript
[Year] => 2010
[Version] =>7
)
)
[1]=>Array
(
[0] => Array
(
[Id] => 1
[Title] => Html
[Author] => peter
)
[1] => Array
(
[Id] => 1
[Title] => Asp
[Author] => john
)
)
[2]=>Array
(
[0] => Array
(
[Id] => 1
[Title] => Html
[Year] => 2011
[Page] =>40
)
[1] => Array
(
[Id] => 1
[Title] => Asp
[Year] => 2010
[Page] =>220
)
)
table has 6 fields id , title , year , author ,version and page .
$this ->table->create(array(
'id' => $data['Id'] ,
'title' => $data['Title'] ,
'year' => $data['Year'] ,
'author' => $data['Author'],
'version' => $data['Version'] ,
'page' => $data['Page'] ,
))
how to insert null to table when variable is not defined . for example like
undefined($data['Version']) ? null : $data['Version'] !!!!!! a php functions or any possible solutions ?
Thanks
You probably will be able to:
foreach($data as $row)
{
$info = array(
'id' => $this->getData($row, 'Id'),
'title' => $this->getData($row, 'Title'),
'year' => $this->getData($row, 'Year'),
'author' => $this->getData($row, 'Author'),
'version' => $this->getData($row, 'Version'),
'page' => $this->getData($row, 'Page'),
);
$this->table->create(array_filter($info));
}
public function getData($data, $key)
{
return isset($data[key]) ? $data[key] : null;
}

Yii cookies remember password

I create cookies authentication in action login but in another controller or another action, i call it but it just echo user'name and nothing else.
For example:
class LoginController extends Controller
{
public function actionIndex()
{
if (isset($_POST['UserLogin'])) {
$username = $_POST['UserLogin']['email'];
$password = $_POST['UserLogin']['password'];
$dataLogin = Users::model()->findByAttributes(array('email'=>$username, 'password'=>md5($password), 'role'=>1, 'status'=>1));
if($dataLogin == NULL) {
Yii::app()->user->setFlash('login_mess',Yii::t('app','Invalid email or password!'));
$this->redirect($_SERVER['HTTP_REFERER']);
}
else{
Yii::app()->session['Authentication'] = $dataLogin;
$cookies = new CHttpCookie('authentication', $dataLogin);
$cookies->expire = time() + 7*24*60*60;
Yii::app()->request->cookies['authentication'] = $cookies;
$this->redirect('/profile');
}
}
}
public function actionLogout()
{
echo "<pre>";
print_r(Yii::app()->request->cookies['authentication']);die;
echo "</pre>";
Yii::app()->session['Authentication'] = null;
$this->redirect('/');
}
}
In actionLogout i try print that cookies:
CHttpCookie Object
(
[name] => authentication
[value] => Bàn Chân Trần
[domain] =>
[expire] => 0
[path] => /
[secure] =>
[httpOnly] =>
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
but i expect:
CHttpCookie Object
(
[name] => authentication
[value] => Users Object
(
....
[_new:CActiveRecord:private] =>
[_attributes:CActiveRecord:private] => Array
(
[id] => 4
[name] => Bàn Chân Trần
[email] => hongquan156#gmail.com
[phone_code] => +44
[phone] => 0988888888
[country] => 2
[language] => vi
[password] =>
[role] => 1
[device_token] =>
[active_key] => 9D9T7n1oWlFiRy1EJ3ULDVlBDtw4VQWT
[status] => 1
[token] =>
[payment] => 0
[created] =>
)
[_related:CActiveRecord:private] => Array
(
)
[_c:CActiveRecord:private] =>
[_pk:CActiveRecord:private] => 4
[_alias:CActiveRecord:private] => t
[_errors:CModel:private] => Array
(
)
[_validators:CModel:private] =>
[_scenario:CModel:private] => update
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
[domain] =>
[expire] => 1345801074
[path] => /
[secure] =>
[httpOnly] =>
[_e:CComponent:private] =>
[_m:CComponent:private] =>
)
How can I reslove this problem... thanks!
Since you're storing an object to cookie, you mus serialize it before storing it to cookie, and unserialize it after retrieving it :
$cookies = new CHttpCookie('authentication', serialize($dataLogin));
print_r(unserialize(Yii::app()->request->cookies['authentication']));die;

How do I extract the details from a PHP object?

I'm working with an object that returns information in a format like the following... I've removed some details where the information gets a little repetitive to save some space.
ISC_QUOTE_ITEM Object (
[type:protected] => 1
[id:protected] => 4cff5974138c9
[hash:protected] => cd2a8393493a286cf1fe97d92a947719
[productData:protected] =>
[productId:protected] => 28
[variationId:protected] => 0
[variationOptions:protected] => Array ( )
[quantity:protected] => 1
[originalOrderQuantity:protected] => 0
[name:protected] => Mac Pro
[basePrice:protected] => 2799.0000
[sku:protected] =>
[wrapping:protected] => Array ( )
[configuration:protected] => Array ( )
[eventDate:protected] => Array ( )
[eventName:protected] =>
[parentId:protected] =>
[quote:protected] => ISC_QUOTE Object (
[customerId:protected] =>
[customerGroupId:protected] =>
[discounts:protected] => Array ( )
[addresses:protected] => Array (
[4cff2f55b0fcd] => ISC_QUOTE_ADDRESS_SHIPPING Object (
[shippingMethod:protected] =>
[shippingCache:protected] =>
[handlingCost:protected] => 0
[quote:protected] => ISC_QUOTE Object *RECURSION*
[type:protected] => shipping
[id:protected] => 4cff2f55b0fcd
[firstName:protected] => asd
[lastName:protected] => asd
[company:protected] => asd
[phone:protected] => asd
[email:protected] => sz#gmail.com
[address1:protected] => asd
[address2:protected] =>
[zip:protected] => 62990
[city:protected] => asd
[state:protected] => Array (
[id] => 0
[iso2] =>
[name] => asd
)
[country:protected] => Array (
[id] => 99
[name] => India
[iso2] => IN
)
[cachedTotals:protected] => Array ( )
[customFields:protected] => Array ( )
[saveAddress:protected] =>
[customerAddressId:protected] => 0
)
[4cff2f55cc2b6] => ISC_QUOTE_ADDRESS Object (
[handlingCost:protected] =>
[quote:protected] => ISC_QUOTE Object *RECURSION*
[type:protected] => billing
[id:protected] => 4cff2f55cc2b6
[firstName:protected] => asd
[lastName:protected] => asd
[company:protected] => asd
[phone:protected] => asd
[email:protected] => sz#gmail.com
[address1:protected] => asd
[address2:protected] =>
[zip:protected] => 62990
[city:protected] => asd
[state:protected] => Array (
[id] => 0
[iso2] =>
[name] => asd
)
[country:protected] => Array (
[id] => 99
[name] => India
[iso2] => IN
)
[cachedTotals:protected] => Array ( )
[customFields:protected] => Array ( )
[saveAddress:protected] =>
[customerAddressId:protected] => 0
)
)
[coupons:protected] => Array ( )
[giftCertificates:protected] => Array ( )
[items:protected] => Array (
[0] => ISC_QUOTE_ITEM Object (
[type:protected] => 1
[id:protected] => 4cff2f55b11d2
[hash:protected] => 587e0ee453488554f782b20f3c928916
[productData:protected] =>
[productId:protected] => 24
[variationId:protected] => 0
[variationOptions:protected] => Array ( )
[quantity:protected] => 3
[originalOrderQuantity:protected] => 0
[name:protected] => Apple iPod Socks
[basePrice:protected] => 29.0000
[sku:protected] =>
[wrapping:protected] => Array ( )
[configuration:protected] => Array ( )
[eventDate:protected] => Array ( )
[eventName:protected] =>
[parentId:protected] =>
[quote:protected] => ISC_QUOTE Object *RECURSION*
[discounts:protected] => Array ( )
[isCustomPrice:protected] =>
[addressId:protected] => 4cff2f55b0fcd
[cachedTotals:protected] => Array ( )
[inQuote:protected] => 1
[inventoryChecking:protected] => 1
[weight:protected] => 1.0000
[fixedShippingCost:protected] => 0.0000
)
[1] => ISC_QUOTE_ITEM Object (
[type:protected] => 1
[id:protected] => 4cff58167d29f
[hash:protected] => 1da6aa898763172b8f7b9d8097d37f42
[productData:protected] =>
[productId:protected] => 23
[variationId:protected] => 0
[variationOptions:protected] => Array ( )
[quantity:protected] => 1
[originalOrderQuantity:protected] => 0
[name:protected] => Apple In-Ear Headphones with Remote and Mic
[basePrice:protected] => 79.0000
[sku:protected] =>
[wrapping:protected] => Array ( )
[configuration:protected] => Array ( )
[eventDate:protected] => Array ( )
[eventName:protected] =>
[parentId:protected] =>
[quote:protected] => ISC_QUOTE Object *RECURSION*
[discounts:protected] => Array ( )
[isCustomPrice:protected] =>
[addressId:protected] => 4cff2f55b0fcd
[cachedTotals:protected] => Array ( )
[inQuote:protected] => 1
[inventoryChecking:protected] => 1
[weight:protected] => 2.0000
[fixedShippingCost:protected] => 0.0000
)
[2] => ISC_QUOTE_ITEM Object *RECURSION*
)
[isSplitShipping:protected] =>
[cachedTotals:protected] => Array ( )
[hasFreeShipping:protected] =>
[appliedStoreCredit:protected] => 0
[appliedDiscountRules:protected] => Array ( )
[customerMessage:protected] =>
[staffNotes:protected] =>
[orderStatus:protected] => 0
[orderId:protected] =>
[discountsEnabled:protected] => 1
)
[discounts:protected] => Array ( )
[isCustomPrice:protected] =>
[addressId:protected] => 4cff2f55b0fcd
[cachedTotals:protected] => Array ( )
[inQuote:protected] => 1
[inventoryChecking:protected] => 1
[weight:protected] => 3.0000
[fixedShippingCost:protected] => 0.0000
)
I need to extract details such as ID, name, etc. so that it reads 4cff5974138c9, Mac Pro, etc. respectively. How can I do that?
You need to call a function of that class (of which ISC_QUOTE_ITEM is an object - say MyClass ) which returns the protected variable
Edit
It can be accessed. I was correct.
Like this -
Add a function inside your class MyClass if you don't already have one.
public function getName()
{
return $this->name;
}
And then in your code you can do this:-
echo $obj->getName();
Read http://php.net/manual/en/language.oop5.visibility.php for details on visibility of types of variables
You can do this by using the reflector construct:
Example taken from http://www.php.net/manual/en/reflectionclass.getproperties.php:
<?php
class Foo {
public $foo = 1;
protected $bar = 2;
private $baz = 3;
}
$foo = new Foo();
$reflect = new ReflectionClass($foo);
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED);
foreach ($props as $prop) {
print $prop->getName() . "\n";
}
var_dump($props);
?>
As you can see also the protected $bar is returned :) And have a look at http://www.php.net/manual/en/reflectionproperty.getvalue.php#98643 there it is shown how to return the value of a protected attribute.

Categories