How distinguish between pages and applications? - facebook

Using php-sdk I obtain pages in this way:
$array = $this->instance->api(
'/' . $fb_user . '/accounts', 'GET', array('access_token' => $access_token)
);
Now var_dump($array['data']); gives me:
array (
0 =>
array (
'name' => 'my name 1',
'access_token' => '***',
'category' => 'Electronics',
'id' => '***',
),
1 =>
array (
'name' => 'my name 2',
'access_token' => '***',
'category' => 'Application',
'id' => '***',
)
Only the first item is a page, the second is an app. Any way to distinguish them or improve this snippet?
EDIT: Using the category field seems a too weak procedure.

If you don't want to rely on "category", you need to fetch informations from these ids.
Run a query for:
https://graph.facebook.com/[page_or_app_id]
Among other things, there will be field:
{
[...]
"type": "application"
]
or:
{
[...]
"type": "page"
]
To avoid long responses, you may consider Batch Requests as described:
http://developers.facebook.com/docs/reference/api/batch/ (general instruction)
Batch calls with Facebook Graph API & PHP (php examples)

Give that the category says "Application", I'd use the category property to filter with.
Alternatively, if as you say using category is too weak for you. You can use strong FQL. SELECT display_name FROM application WHERE app_id='{the id from your me/accounts}' and see if you get back an object or not.

Related

Can someone provide a php sample using nusoap/sugarcrm api to create an acct/lead in sugarcrn?

Can someone provide a sample code chunk of php using the sugarcrm API/nusoap for adding creating an acct. and then linking a lead to the acct?
I've got a sample function that adds a lead, and I can see how to create an acct, but I can't see how to tie a lead to the acct, to simulate the subpanel process in the sugarcrm acct/subpanel process.
thanks
// Create a new Lead, return the SOAP result
function createLead($data)
{
// Parse the data and store it into a name/value list
// which will then pe passed on to Sugar via SOAP
$name_value_list = array();
foreach($data as $key => $value)
array_push($name_value_list, array('name' => $key, 'value' => $value));
// Fire the set_entry call to the Leads module
$result = $this->soap->call('set_entry', array(
'session' => $this->session,
'module_name' => 'Leads',
'name_value_list' => $name_value_list
));
return $result;
}
$result = $sugar->createLead(array(
'lead_source' => 'Web Site',
'lead_source_description' => 'Inquiry form on the website',
'lead_status' => 'New',
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'email1' => $_POST['email'],
'description' => $_POST['message']
));
You need to find the ID for the account and assign that ID to whatever the account_id field name is in the Lead Module. I have run into a couple things like this before and I have found it easier to go straight to the Sugar database. So, write a statement that will return the account is, for example: SELECT id WHERE something_in_the_account_table = something else;
Then you can assign that id in your $result array. I hope it helps. I didn't have any code or documentation in front of me or I would have helped more.

Zend Session webshop some items are just not stored

I have a weird problem where I want to store products from my shop in a session. This works well, except for some products. The shop is part of a cms where all products are rendered the same way. When someone adds a product to the shop this will be serialized and send with ajax to a script.
Almost all items are stored, but for some reason some items don't get stored. I can't find a connection or anything and I don't get any errors returned.
So this is the code that stores the product in a session:
$storeItemNumber = (string)$post['itemcode'];
$storeItem = array($storeItemNumber => array(
'title' => $post['title'],
'price' => $post['price'],
'quantity' => $post['quantity']
)
);
$shopSession->$storeItemNumber = $storeItem;
This is an example of a product that gets stored:
array('010101000' => array(
'title' => 'Product title - 15',
'price' => '28.95',
'quantity' => '1',
));
This is an example of a product that doesn't get stored:
array('400002001' => array(
'title' => 'Product title - Pink',
'price' => '5.50',
'quantity' => '1',
));
I already checked if the data gets through alright and it does right until the saving of it in a session.
What could possibly be a reason?
Had a look with a friend of mine and we both concluded it had something to do with the numbers. So I changed the function to the following and Now I'm able to add all the products without a problem.
I'll leave this question open for the time being, because I'm really curious why some numbers are stored and others not and what is a better/cleaner solution then mine.

how to set album privacy settings using facebook graph api

Is there any way to change the Facebook album privacy settings with graph api?
I'm trying to find out, but all I could found is how to get the privacy settings using fql, but not to set.
I'm creating the album as follow
$postdata = http_build_query(array(
'name' => $album_name,
'message' => $album_description
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=>
'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
) $context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false, $context));
$albumid = $result->id;
Now if I add privacy=>"value", it gives $albumid=null.
I'm not sure where I need to add privacy parameter.
When you create an album, you can send these parameters in post request.
name, message, location and privacy.
Value of privacy field can be set like this,
privacy={value: "CUSTOM"} (send this as post parameter)
The value field may specify one of the following strings:
EVERYONE, ALL_FRIENDS, NETWORKS_FRIENDS, FRIENDS_OF_FRIENDS, CUSTOM .
As facebook docs sucks, there's no mention about it on albums object page.
However, you can read it on post object.
Edit: (after comments)
In php sdk you can do something like this,
$ret_obj = $facebook->api('me/albums', 'POST',
array(
'privacy' => '{value: "CUSTOM"}',
'location' => 'India'
));
The document of creating an album is put in https://developers.facebook.com/docs/reference/api/user/#albums
Privacy setting is a json-style string. So you could create an array() and use json_encode() to generate it.
with php sdk it is also possible as php style #kaur
$ret_obj = $facebook->api('/me/albums/', 'POST', array(
'source' => '#' . $photo,
'message' => 'Picture uploaded',
'location' => 'Goran',
'privacy'=> array('value'=>'EVERYONE'), //'privacy'=> '{value: "EVERYONE"}', //worked too!! SELF, ALL_FRIENDS, EVERYONE
)
);

Zend_Validate_Db_RecordExists against 2 fields

I usualy use Zend_Validate_Db_RecordExists to update or insert a record. This works fine with one field to check against. How to do it if you have two fields to check?
$validator = new Zend_Validate_Db_RecordExists(
array(
'table' => $this->_name,
'field' => 'id_sector,day_of_week'
)
);
if ($validator->isValid($fields_values['id_sector'],$fields_values['day_of_week'])){
//true
}
I tried it with an array and comma separated list, nothing works... Any help is welcome.
Regards
Andrea
To do this you would have to extend the Zend_Validate_Db_RecordExists class.
It doesn't currently know how to check for the existence of more than one field.
You could just use two different validator instances to check the two fields separately. This is the only work around that I can see right now besides extending it.
If you choose to extend it then you'll have to find some way of passing in all the fields to the constructor ( array seems like a good choice ), and then you'll have to dig into the method that creates the sql query. In this method you'll have to loop over the array of fields that were passed in to the constructor.
You should look into using the exclude parameter. Something like this should do what you want:
$validator = new Zend_Validate_Db_RecordExists(
array(
'table' => $this->_name,
'field' => 'id_sector',
'exclude' => array(
'field' => 'day_of_week',
'value' => $fields_values['day_of_week']
)
);
The exclude field will effectively add to the automatically generated WHERE part to create something equivalent to this:
WHERE `id_sector` = $fields_values['id_sector'] AND `day_of_week` = $fields_values['day_of_week']
Its kind of a hack in that we're using it for the opposite of what it was intended, but its working for me similar to this (I'm using it with Db_NoRecordExists).
Source: Zend_Validate_Db_NoRecordExists example
Sorry for the late reply.
The best option that worked for me is this:
// create an instance of the Zend_Validate_Db_RecordExists class
// pass in the database table name and the first field (as usual)...
$validator = new Zend_Validate_Db_RecordExists(array(
'table' => 'tablename',
'field' => 'first_field'
));
// reset the where clause used by Zend_Validate_Db_RecordExists
$validator->getSelect()->reset('where');
// set again the first field and the second field.
// :value is a named parameter that will be substituted
// by the value passed to the isValid method
$validator->getSelect()->where('first_field = ?', $first_field);
$validator->getSelect()->where('second_field = :value', $second_field);
// add your new record exist based on 2 fields validator to your element.
$element = new Zend_Form_Element_Text('element');
$element->addValidator($validator);
// add the validated element to the form.
$form->addElement($element);
I hope that will help someone :)
Although, I would strongly recommend a neater solution which would be to extend the Zend_Validate_Db_RecordExists class with the above code.
Enjoy!!
Rosario
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
'validators' => array('EmailAddress', $obj= new Zend_Validate_Db_NoRecordExists(array('adapter'=>$dbAdapter,
'field'=>'email',
'table'=>'user',
'exclude'=>array('field'=>'email','value'=>$this->_options['email'], 'field'=>'is_deleted', 'value'=>'1')
))),
For those using Zend 2, If you want to check if user with given id and email exists in table users, It is possible this way.
First, you create the select object that will be use as parameter for the Zend\Validator\Db\RecordExists object
$select = new Zend\Db\Sql\Select();
$select->from('users')
->where->equalTo('id', $user_id)
->where->equalTo('email', $email);
Now, create RecordExists object and check the existence this way
$validator = new Zend\Validator\Db\RecordExists($select);
$validator->setAdapter($dbAdapter);
if ($validator->isValid($username)) {
echo 'This user is valid';
} else {
//get and display errors
$messages = $validator->getMessages();
foreach ($messages as $message) {
echo "$message\n";
}
}
This sample is from ZF2 official doc
You can use the 'exclude' in this parameter pass the second clause that you want to filter through.
$clause = 'table.field2 = value';
$validator = new Zend_Validate_Db_RecordExists(
array(
'table' => 'table',
'field' => 'field1',
'exclude' => $clause
)
);
if ($validator->isValid('value') {
true;
}
I am using zend framework v.3 and validation via InputFilter(), it uses same validation rules as zend framework 2.
In my case I need to check, if location exists in db (by 'id' field) and has needed company's id ('company_id' field).
I implemented it in next way:
$clause = new Operator('company_id', Operator::OP_EQ, $companyId);
$inputFilter->add([
'name' => 'location_id',
'required' => false,
'filters' => [
['name' => 'StringTrim'],
['name' => 'ToInt'],
],
'validators' => [
[
'name' => 'Int',
],
[
'name' => 'dbRecordExists',
'options' => [
'adapter' => $dbAdapterCore,
'table' => 'locations',
'field' => 'id',
'exclude' => $clause,
'messages' => [
'noRecordFound' => "Location does not exist.",
],
]
],
],
]);
In this case validation will pass, only if 'locations' table has item with columns id == $value and company_id == $companyId, like next:
select * from location where id = ? AND company_id = ?

How do I use Perl's WWW::Facebook::API to publish to a user's newsfeed?

We use Facebook Connect on our site in conjunction with the WWW::Facebook::API CPAN module to publish to our users newsfeed when requested by the user.
So far we've been able to successfully update the user's status using the following code:
use WWW::Facebook::API;
my $facebook = WWW::Facebook::API->new(
desktop => 0,
api_key => $fb_api_key,
secret => $fb_secret,
session_key => $query->cookie($fb_api_key.'_session_key'),
session_expires => $query->cookie($fb_api_key.'_expires'),
session_uid => $query->cookie($fb_api_key.'_user')
);
my $response = $facebook->stream->publish(
message => qq|Test status message|,
);
However, when we try to update the code above so we can publish newsfeed stories that include attachments and action links as specified in the Facebook API documentation for Stream.Publish, we have tried about 100 different ways without any success.
According to the CPAN documentation all we should have to do is update our code to something like the following and pass the attachments & action links appropriately which doesn't seem to work:
my $response = $facebook->stream->publish(
message => qq|Test status message|,
attachment => $json,
action_links => [#links],
);
For example, we are passing the above arguments as follows:
$json = qq|{ 'name': 'i\'m bursting with joy', 'href': ' http://bit.ly/187gO1', 'caption': '{*actor*} rated the lolcat 5 stars', 'description': 'a funny looking cat', 'properties': { 'category': { 'text': 'humor', 'href': 'http://bit.ly/KYbaN'}, 'ratings': '5 stars' }, 'media': [{ 'type': 'image', 'src': 'http://icanhascheezburger.files.wordpress.com/2009/03/funny-pictures-your-cat-is-bursting-with-joy1.jpg', 'href': 'http://bit.ly/187gO1'}] }|;
#links = ["{'text':'Link 1', 'href':'http://www.link1.com'}","{'text':'Link 2', 'href':'http://www.link2.com'}"];
The above, nor any of the other representations we tried seem to work. I'm hoping some other perl developer out there has this working and can explain how to create the attachment and action_links variables appropriately in Perl for posting to the Facebook news feed through WWW::Facebook::API.
Thanks in advance for your help!
I think the problem is that your JSON string might be invalid. I was able to get it to work by just using JSON::Any to serialize a Perl data structure instead of building the JSON string manually. (WWW::Facebook::API uses JSON::Any under the hood; it would be nice if it could take a Perl data structure instead of a JSON string. I will try to submit a patch this weekend.)
use WWW::Facebook::API;
use JSON::Any;
my $j = JSON::Any->new;
my $fb = WWW::Facebook::API->new(
desktop => 0,
api_key => $api_key,
secret => $secret,
session_key => $session,
session_expires => $expires,
session_uid => $fb_uid
);
my $res = $fb->stream->publish(
message => 'Test message',
attachment => $j->objToJson(
{ name => 'Foo bar baz',
href => 'http://www.google.com/',
description => "this is a thing"
} ),
action_links => $j->objToJson(
[ { text => 'action link text',
href => 'http://www.foobar.com/'
} ] )
);
The result:
http://www.friedo.com/fb_attach.jpg