Google Api - Get users details - php

I am using the Google Api PHP Client.
I want to get the details of the user who has logged in.
I am setting the following Scopes
$client->setScopes('https://www.googleapis.com/auth/userinfo.profile');
$request = new apiHttpRequest("https://www.googleapis.com/oauth2/v1/userinfo?alt=json");
$userinfo = $client->getIo()->authenticatedRequest($request);
$response = json_encode(simplexml_load_string($userinfo->getResponseBody()));
print "<pre>" . print_r(json_decode($response, true), true) . "</pre>";
But i am not able to get the jSON from the above code.

Wanted to Deleted the Question but didnt find any Answer that uses the PHP Client. so answering my own question
Just by replacing
$response = json_encode(simplexml_load_string($userinfo->getResponseBody()));
by
$response = $userinfo->getResponseBody();

Related

Twilio lookup API not working?

I'm trying to use Twilio's Lookup API to get certain properties of a mobile number via PHP... with very little success:
$twilioClient = new Lookups_Services_Twilio(Credential::TwilioSID, Credential::TwilioToken);
$number = $twilioClient->phone_numbers->get($someNumber);
Note that this is the example code present within their 'Getting Started' page here.
By taking a look at $number in the debugger, I can confirm it is returning something:
The highlighted property of the object is simply recursive with no new information.
Attempting to evaluate $number->phone_number returns null. I have tried this with perhaps half a dozen completely valid numbers now and this is the only response I get.
Attempting to json_encode($number) returns false.
I have no idea why this is not working, but it'd be helpful if I could know what I'm doing wrong.
I would have been also not successful with their code defined so i used CURL to grab their API methods and it worked like a charm for me, you can try following code to get you need
$base_url = "https://lookups.twilio.com/v1/PhoneNumbers/+1XXXXXXXXXX";
$ch = curl_init($base_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$account_sid:$auth_token");
$response = curl_exec($ch);
$response = json_decode($response);
echo "<pre>"; print_r($response); echo "</pre>";
It will return you few parameters (country_code, national_format, carrier)
I'm just gonna go ahead and assume the phone numbers you've tried are neither from the US, nor in international format.
From Twilio's Lookups Quickstart Tutorial:
You'll want to include the country code of the phone number that you would like formatted. If not included, the country code will default to the US.
So your lookup should probably look like:
$number = $twilioClient->phone_numbers->get($someNumber, array('CountryCode' => 'NZ'));
If the phone numbers are from the US, in international format, or if the above still does not work, try whether the lookup succeeds on Twilio's web interface (you'll need the international prefix there).
If it does, your software library might be broken or your Twilio account might have incorrect/broken access rights.
If the web lookup fails as well, you should contact Twilio and report the issue.
Now 9-6-2016 and they still haven't fixed their PHP library...
None the less here is what worked for me. If you want more information like caller name etc you have to enable this in your twilio dashboard first.
require 'includes/twilio/Services/Twilio.php';
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "YOUR-SID";
$token = "YOUR-TOKEN";
$client = new Lookups_Services_Twilio($sid, $token);
// Lookup
$phoneNumber = rawurlencode("(000) 000-0000");
$full_path = $client->phone_numbers->uri . "/$phoneNumber" . "?CountryCode=US&Type=carrier&Type=caller-name";
$number = new $client->phone_numbers->instance_name($client, $full_path);
echo "Caller name:" . $number->caller_name->caller_name;
echo "<br>";
echo "Caller type:" . $number->caller_name->caller_type;
echo "<br>";
echo "Carrier type:" . $number->carrier->type . "\r\n";
echo "<br>";
echo "Carrier name:" . $number->carrier->name;
echo "<br>";
echo "Phone number:" . $number->phone_number;
echo "<br>";
echo "Country code:" . $number->country_code;

How can I fetch data from facebook events using graph API

I want to fetch data from a facebook event using graph API. I wrote the following code :
<?php
$url = 'https://graph.facebook.com/events/1588093858105216';
$contents = file_get_contents( $url );
if( $contents )
{
$data = json_decode( $contents, true );
echo '<pre>';
print_r( $data );
echo '</pre>';
}
?>
My event page address URL is https://www.facebook.com/events/1588093858105216/.
The above code is not working properly. When I am using the same code for a page,e.g., https://www.facebook.com/TangeloTown , it works fine.
<?php
$url = 'https://graph.facebook.com/TangeloTown';
$contents = file_get_contents( $url );
if( $contents )
{
$data = json_decode( $contents, true );
echo '<pre>';
print_r( $data );
echo '</pre>';
}
?>
Also, I am unable to run it on localhost. Please help me to solve this or please tell me any other method to solve this.
Generally, you need an access token to access all graph api. Please check the doc here for event api: https://developers.facebook.com/docs/graph-api/reference/v2.3/event
In your case, if you want to access event, you need a user access token or app access token. To get user access token you check this doc: https://developers.facebook.com/docs/facebook-login/access-tokens in this way, you need to ask user for authorization. But if you only want to access public event, you can simply use your app access token, which can be found here: https://developers.facebook.com/tools/accesstoken/, but the prerequisite is that you need to register a FB app.

facebook access_token request return NOTHING

it's been 5 days that I'm stuck in the implementation of a simple fb login and I'm banging my head like a motherf***r. anyways.
I'm at the point that I'm trying to set the accessToken by hand.
like shown in this post.
https://github.com/facebook/php-sdk/issues/418#issuecomment-2605012
the only problem is that the oauth/access_token call returns nothing
and I'm guessing that it's a pretty messed up behaviour.
this is the snippet I use to make the call
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=".FB_APPID."&redirect_uri=".urlencode($curUrl)."&client_secret=".FB_APPSECRET."&code=".$_GET['code'];
log_to_file("curPageURL: ".$token_url);
$response = file_get_contents($token_url);
log_to_file("resp: ".$respone);
the log_to_file is a custm method that logs the taken message in a file so that I can have the log situation in a tail -f scenario.
what happens here is that the log "resp: ". returns nothing at all.
did anyone face the same problem?
thx in advance. this thing is driving me insane.
and I can officially state that the fb sdk is the most buggy and worse-documented service
I've ever used.
Facebook has the most badly-documented API in the world. I remember the time I used it and couldn't help swearing all the time ! :) Here is a piece of pseudo-code that did work for me. It gains permission from a user to post a link on his wall. I'm just posting it so that maybe you can take some hints and make your code work:
<?php
//A function for cURL operations.
function callFb($url)
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$url = "https://graph.facebook.com/oauth/access_token?client_id=<your_client_id>&redirect_uri=".urlencode("<the_url_where_the_user_is_redirected_after_granting_permission>")."&client_secret=<your_client_secret>";
/* Get access token. */
$access_token = callFb($url);
/* Parse the result to get access token */
$access_token = substr($access_token, strpos($access_token, "=")+1, strlen($access_token));
/* Save access token, if you want to for future.*/
mysql_query("INSERT INTO fb_auth_tokens (id,auth_token) VALUES('$_GET[id]','$auth_token')");
/* Post to users wall */
$apprequest_url = "https://graph.facebook.com/me/feed";
$mymessage="Hello World !";
$parameters = "?access_token=" . $access_token . "&message=" .
urlencode($mymessage) .
"&link=".urlencode("<link_that_you_want_to_post>").
"&description=<description_of_the_link>".
"&method=post";
$myurl = $apprequest_url . $parameters;
$result = callFb($myurl);
// Thy shall be done. :)

Amazon.com MWS Integration

I am currently developing a very basic site which will, at this time, simply display order information from Amazon's Marketplace.
I have all of the MWS Security Credentials.
I have downloaded and reviewed, with much confusion, the PHP Client Library.
I am kind of new to PHP but I feel like I can handle this project.
I need to know how to install and access information from this API. I feel like I've tried everything. Amazon does not supply enough information to get this going. They make it sound like it takes 5 or 6 easy steps and you can access your information; this is not true.
Is there a detailed tutorial on MWS? I need as much information as possible. If you can help me out, maybe outline the steps required to get it going, that would be very appreciated!!!! I'm pulling my hair out over this. Thanks again
A rough file to get you started. This is taken from several pages, including this one from #Vaidas. I don't have links yet, sorry. My only contribution is to put this together in one place.
None of the PHP code Amazon supplied worked for me out of the box. I'm assuming you have XAMPP with cURL or an equivalent environment. This code SHOULD work out of the box to get you started on what needs to happen. Just plug in your credentials.
<?php
$param = array();
$param['AWSAccessKeyId'] = 'YourAccessKeyID';
$param['Action'] = 'GetLowestOfferListingsForASIN';
$param['SellerId'] = 'YourSellerID';
$param['SignatureMethod'] = 'HmacSHA256';
$param['SignatureVersion'] = '2';
$param['Timestamp'] = gmdate("Y-m-d\TH:i:s.\\0\\0\\0\\Z", time());
$param['Version'] = '2011-10-01';
$param['MarketplaceId'] = 'YourMarketplaceID';
$param['ItemCondition'] = 'new';
$param['ASINList.ASIN.1'] = 'B00C5XBAOA';
$secret = 'YourSecretKey';
$url = array();
foreach ($param as $key => $val) {
$key = str_replace("%7E", "~", rawurlencode($key));
$val = str_replace("%7E", "~", rawurlencode($val));
$url[] = "{$key}={$val}";
}
sort($url);
$arr = implode('&', $url);
$sign = 'GET' . "\n";
$sign .= 'mws.amazonservices.com' . "\n";
$sign .= '/Products/2011-10-01' . "\n";
$sign .= $arr;
$signature = hash_hmac("sha256", $sign, $secret, true);
$signature = urlencode(base64_encode($signature));
$link = "https://mws.amazonservices.com/Products/2011-10-01?";
$link .= $arr . "&Signature=" . $signature;
echo($link); //for debugging - you can paste this into a browser and see if it loads.
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
echo('<p>' . $response . '</p>');
print_r('<p>' . $info . '</p>');
?>
Please note that it is VITAL to have the
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
line, at least in my case. CURL was working fine for any page except for the MWS page (it was just giving me a blank page with -1s in the info, and it took me most of a day to figure out I needed that line. It's in the MWS forums somewhere.
For good measure, here's a link to MWS ScratchPad.
Once I get a better handle on working with MWS maybe I'll do a tutorial. Or someone who is better at HTML and has a need for more of the features could do it.
in case you still didn't figure out how to do this, follow these steps
read the Developer Guide
read the Reports API Reference
RequestReport with some ReportType that will return order data (page 51 or so, look the reports api reference)
you can test this with the MWS Scratchpad
you can also post to the Amazon MWS community forum to get additional help
you can even write to the Amazon Tech Support
hope this helps you and other users.
Amazon provides some great sample code at https://developer.amazonservices.com/. I've successfully used their code for my PHP applications.
I agree. It was a nightmare to figure out the MWS API.
Some changes to #Josiah's method to make it work for other marketplaces:
Line:
$sign .= 'mws.amazonservices.com' . "\n";
Change to: your correct MWS endpoint. List here http://docs.developer.amazonservices.com/en_US/dev_guide/DG_Endpoints.html - it'll match your marketplace ID, which could be something like this:
$sign .= 'mws-eu.amazonservices.com' . "\n";
and UK marketplace ID for UK site.
Line:
$link = "https://mws.amazonservices.com/Products/2011-10-01?";
Again, change the start of the url in line with above.
This'll probably give you straight text output in a browser (view source for xml). For XML visible output (easier for checking) do this:
Add an XML content type line to top of file:
header('Content-type: application/xml');
Then comment out:
echo($link);
and
print_r('<p>' . $info . '</p>');
Implementing MWS is easy if you follow the right steps:
1-Download the codebase library from the https://developer.amazonservices.com/ as per your preferred language.
2-Set your seller mws credentials in config.php file under sample folder so that same can be used while running the specific file under the sample folder like: RequestReportSample.php and set the report type and endpoint url for specific seller domain.
3- You can then check submitted request status from scratchpad.
4- You can use GetReportSample file to get the order report data and use the same as per your need.
You can follow the reference as well http://prashantpandeytech.blogspot.com/2015/03/mws-amazon-marketplace-web-service-api.html

need php api help

hi friends i am trying to solve how i can access the halo reach api for get states bungie has released a api for this here is the link http://www.bungie.net/fanclub/statsapi/Group/Resources/Article.aspx?cid=545064
how can i access this service via php and display some stats need help thanks
i am trying like this
<?php
require_once('lib/nusoap.php');
$wsdl = "http://www.bungie.net/api/reach/ReachApiJson.svc?wsdl";
$client = new soapclient($wsdl, 'wsdl');
$parameters['parameters']['apikey'] = "xxx";
$result = $client->call("GetGameMetadata", $parameters);
?>
Consuming JSON in PHP is pretty simple.
<?PHP
$uri = 'http://www.bungie.net/api/reach/reachapijson.svc/game/metadata/xxx';
if (! $json = file_get_contents($uri)){ //cURL can be faster and more flexible, but this ought to work
trigger_error('API call failed!');
}
if (! $result = json_decode($json)){
trigger_error('API returned bad data?');
//maybe log some stuff here, so you can debug.
}
print_r($result); //see what you got.

Categories