People API searchContacts using google people service - google-people-api

I have tried adding a new contact using Google People API with PHP client people service and it worked fine. I have used following code,
$service = new Google_Service_PeopleService($client);
$person = new Google_Service_PeopleService_Person();
$name = new Google_Service_PeopleService_Name();
$name->setGivenName($_name);
$person->setNames($name);
$phone1 = new Google_Service_PeopleService_PhoneNumber();
$phone1->setValue($_phone);
$phone1->setType('home');
$person->setPhoneNumbers($phone1);
$exe = $service->people->createContact($person)->execute;
Now I want to check if particular email already exists so tried searchContacts method of API. It works fine in API explorer.
ref:
https://developers.google.com/people/api/rest/v1/people/searchContacts
But when have tried with following code,
$service = new Google_Service_PeopleService($client);
$optParams = array('query'=>$_email,'pageSize' => 10, 'readMask' => 'emailAddresses', 'key'=>'XXXX');
try{
$rslt = $service->people->searchContacts($optParams);
} catch(Exception $ex) {
echo $e->getMessage();
}
i am getting following error,
PHP Fatal error: Call to undefined method Google_Service_PeopleService_Resource_People::searchContacts()
Looking at the error i am sure that the way to call the method is wrong. I have searched but not able to get proper documentation in guiding in that direction.
Any help will be appreciated.
Thanks

Looks like we cannot call searchContacts using people service object so i have used curl json to get it worked. I have used following code if anyone needs.
$url_parameters = array('query'=>$_email,'pageSize' => 10, 'readMask' => 'names,phoneNumbers,emailAddresses,organizations', 'key'=>'XXXXX');
$url_calendars = 'https://people.googleapis.com/v1/people:searchContacts?'. http_build_query($url_parameters);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_calendars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = json_decode(curl_exec($ch), true);
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($http_code != 200)
throw new Exception('Error : Failed to search contact');
return $data;
Thanks

Related

AccountKIt-Facebook Set up a page to handle successful logins and exchange authorization codes for access tokens

I have followed everything said accountkit docs:
<?php
// Initialize variables
$app_id = '<facebook_app_id>';
$secret = '<account_kit_app_secret>';
$version = '<account_kit_api_version>'; // 'v1.1' for example
// Method to send Get request to url
function doCurl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = json_decode(curl_exec($ch), true);
curl_close($ch);
return $data;
}
// Exchange authorization code for access token
$token_exchange_url = 'https://graph.accountkit.com/'.$version.'/access_token?'.
'grant_type=authorization_code'.
'&code='.$_POST['code'].
"&access_token=AA|$app_id|$secret";
$data = doCurl($token_exchange_url);
$user_id = $data['id'];
$user_access_token = $data['access_token'];
$refresh_interval = $data['token_refresh_interval_sec'];
// Get Account Kit information
$me_endpoint_url = 'https://graph.accountkit.com/'.$version.'/me?'.
'access_token='.$user_access_token;
$data = doCurl($me_endpoint_url);
$phone = isset($data['phone']) ? $data['phone']['number'] : '';
$email = isset($data['email']) ? $data['email']['address'] : '';
?>
The problem is the function doCurl is not returning anything, when I try to var_dump($data) the value is false.
What can I know what to do next to understand what's going on...
I am developing on the local computer using wamp, so I first checked that cURL is activated on php
After confirming that I looked for article to understand how to use cURL to send requests from php and I found this wonderfull tutorial
After reading I focus on the result returned by curl_exec since mine was returning false and the article says in that case there is error, I use the command to display what the error can be
Error displayed was : curl-error-60-ssl-certificate-unable-to-get-local-issuer-certificate
I search solution on stackoverflow and this question has the solution.
MY problem is solved.

Making a call to my webservice via curl doesn't work

I have a simple function to test my webservice via POST like this:
function service(){
$service_url = 'http://example.com/example_endpoint/user';
$curl = curl_init($service_url);
$header = array(
'Content-Type: application/x-www-form-urlencoded'
);
$curl_post_data = array(
"name" => "name_test",
"mail" => "name_test#example.com",
"pass" => "123",
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$curl_response = curl_exec($curl);
curl_close($curl);
$xml = new SimpleXMLElement($curl_response);
}
The webservice is specting the parameters on the array and the Content-Type is suppoused to be application/x-www-form-urlencoded but when I run the function in my browser and check the "Network" tab on "inspect element", there is a call to my webservice via GET despite of the fact that I'm setting the option to be POST
and the Content-Type keeps on text/html
This webservice allows to create users with the parameters inside the array $curl_post_data
I use the add-on "Poster" on Mozilla to call my webservice and it's succesfull, but when I call the function above, it doesn't work ¿How could I implement this function to make the correct call?
In Network tab on your browser you will not see POST as curl is posting this. Network tab shows activity from client side (Browser). Your data posting is happening by server through CURL.
Add this code to properly url encode data
$curl_post_data_string = '';
//url-ify the data for the POST
foreach($curl_post_data as $key => $value) {
$curl_post_data_string .= $key.'='.$value.'&';
}
rtrim($curl_post_data_string, '&');
And change this line
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
Keep in mind to see what you got after hitting a URL in CURL you need to print $curl_response
echo $curl_response;
This is a proper example of posting data using CURL
http://davidwalsh.name/curl-post

*Malformed access token - but it's the token i get (PHP)

i have this problem with my FB-App
i have 2 files..
1) the mainApp.php .. and ..
2) the asyncApp.php .. (which processes data, delivers content & shit..)
and now..
when a user logs in, everything works fine.. i get the access-token and save it to a SESSION-Var..(only mainApp.php is processed here).. but..
when i call the async.php via jquery.load() e.g. .. i always get the
{"error":{"message":"Malformed access token .. oO
but the token is the same i get from FB in the mainApp.php.. :(
mainApp:
this way i get the Token..
if(isset($_GET["code"])) {
$code = $_GET["code"];
$url = 'https://graph.facebook.com/oauth/access_token?client_id='.$appID.'&redirect_uri='.urlencode($appRedirectURI).'&client_secret='.$appSecret.'&code='.$code;
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$url);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,6);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if(strpos($buffer, 'access_token=') === 0) {
//if you requested offline acces save this token to db for use later
$token = str_replace('access_token=', '', $buffer);
$_SESSION['fbToken'] = $token;
later i call the async.php which should do a feed on the users wall..
$attachment = array(
'access_token' => $_SESSION['fbToken'],
'message' => 'dfdfdf',
'name' => 'sdfdsf',
'link' => 'http://www.mbla.de',
'description' => 'sdfdsf',
'picture'=> '',
'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/me/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
would be nice if someone could help me.. i'm suffering almost 2 weeks now.. :(
Welcome to Stack Overflow!
What happens when you call async.php directly, does it work?
What happens when you have the script print_r() the contents of $_SESSION, is it populated?
What about if you do it through an ajax call, and inspect the output through Firebug/Chrome Inspector?
Looking at the code attached (async.php), it looks like you're not passing in the parameters properly to Facebook. The curl option CURLOPT_POSTFIELDS does not take an array as an argument. Instead, it should be constructed as a query string:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'param1=value&param2=value&param3=value');
For ease, I tend to do this instead:
curl_setopt($ch, CURLOPT_POSTFIELDS, htmlspecialchars(http_build_query(array(
"param1" => 'value',
"param2" => 'value',
"param3" => 'value'
))));

Cannot getUser() in the canvas page using PHP SDK

I am quite confused using the Facebook application in the iframe. I granted permissions to the app and accessed to it, but the application acts very strange. If I try to test myself withe the code:
$app = array(
'appId' => 'yyy',
'secret' => 'xxx',
'cookie' => true
);
$facebook = new Facebook($app);
$user_id = $facebook->getUser();
I get 0. On the other hand, if I use this kind of code:
$canvas_page = 'http://www.bandito.sk/facebook/canvas.php';
$auth_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page)."&scope=user_about_me,user_birthday,email,user_status";
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
I get all what I need. But I just don't understand, the Facebook doesn't even send a signedRequest() in the first example. Do I have to login even tough I use the application inside Facebook as a canvas page?
OK found it on my own
$request_ids = explode(',', $_REQUEST['request_ids']);
print_r($request_ids);
foreach($request_ids AS $request_id){
$request_uri=$request_id."_".$data[user_id];
$ch = curl_init('https://graph.facebook.com/'.$request_uri.'&access_token='.$data[oauth_token]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$response = curl_exec($ch);
var_dump($response)."<br>";
}

Cakephp post request to webservice

I have a web service that works fine with get requests but trying to use post it doesn't seem to work. I have this in my routes:
Router::connect("/products/service_add", array("controller"=>"products", "action" => "service_add", 'seller'=>true, "[method]" => "POST"));
Seller is like an admin. I also have ParseExtensions('xml'). My action is:
function seller_service_add() {
$this->log("hit", 'debug');
$hi = array("message"=>"hey");
$this->set(compact('hi'));
}
and the view is:
<product>
<?php echo $xml->serialize($hi); ?>
</product>
When I try to send a post to the API I just get a debug trace back with timer info. In the same script as I'm doing the post request I'm doing a get and when I run the script I can see the data passed by the API for the get but not for the post request.
The code I'm using to send the post is:
$ch = curl_init();
$data = array('Your' => 'Data');
curl_setopt($ch, CURLOPT_URL, "http://localhost/<project>/products/service_add.xml");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
curl_close($ch);
print_r($result);`
Any ideas on why this isn't working? Any help would be much appreciated.
Sorry if this is a bit verbose!
Update: I've added lines to spit stuff out to the cakephp debug log when actions are hit. I have one entry on the beforeFilter, this entry gets hit and I can see it in the log. The entry in the service_add action is not present in the log so I guess that the action is not being hit?
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
shouldn't this line be
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);