Specifying Parameters in Zend_GData when using an Oauth Token? - php

So, I figured out how to get an access token from Google using the Zend_Oauth library in 1.10. Now lets say I want to get my contacts...
$config = array(
'consumerKey' => 'zzz',
'signatureMethod' => 'HMAC-SHA1',
'consumerSecret' => 'xxx'
);
$token = unserialize($_SESSION['GOOGLE_ACCESS_TOKEN']);
$client = $token->getHttpClient($config);
$client->setMethod(Zend_Http_Client::GET);
// $client->setParameterGet('max-results', '10000');
$gdata = new Zend_Gdata($client);
$gdata->setMajorProtocolVersion(3);
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/full');
// $query->MaxResults=100;
$feed = $gdata->getFeed($query);
$feed is a lovely object with 25 contacts. But if I want to get more in a single pull, there doesn't seem to be a way of specifying max results that works.
If I uncomment client->setParameterGet it's ignored. It works if I specify $client->setUri and use $rawdata = client->request() to get the response, but then other issues crop up with handling the feed data that comes back... like getting it into GData for easy handling.
I've tried $feed = $gdata->importString($rawdata->getBody()) but while $rawdata->getBody() returns what seems to be valid XML, $feed->totalResults throws an error, while it wouldn't if I used $gdata->getFeed($query).
If I uncomment $query->MaxResults=100; use $gdata->getFeed($query) Google returns a 401 with "Unknown authorization header".
So is it possibly to specify parameters while using Zend_GData with an Oauth token? Or am I going to have to build my own requests, then use Zend_Feed (or some other XML/Feed dissector) for parsing?

I totally cannot get the whole list of contacts only 25... parameters do not seem to work using Gdata and query like this:
$http = $token->getHttpClient($oauthOptions);
$gdata = new Zend_Gdata($http, 'MY APP');
$gdata->setMajorProtocolVersion(3);
$gdata->getHttpClient()->setRequestScheme(Zend_Oauth::REQUEST_SCHEME_QUERYSTRING);
$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/full?max-results=10');
$query->setMaxResults(10);
$query->maxResults = 10;
$feed = $gdata->getFeed($query);
so i;m really into finding answers here as well. If either of you gets anything working. please post :-)
thanks

It's a bit tricky mixing a process meant to work with AuthSub with OAuth. I did some digging. So far I can get it to download all my contacts like this...
$client = $token->getHttpClient($config);
$client->setMethod(Zend_Http_Client::GET);
$client->setUri('http://www.google.com/m8/feeds/contacts/default/full/');
$client->setParameterGet('max-results', '10000');
$client->setParameterGet('v','3');
$bfeed = $client->request();
Looks like the primary difference between us is I specify the Feed URL in the $client->setUri('http://www.google.com/m8/feeds/contacts/default/full/'); and set my version differently. But I can get the body() property of $bfeed and it gives me 245k of XML to dissect.
My problem is that when I'm pulling down a single contact's feed via this method, I was getting an error.
I, like you, am trying to figure this out, so please reply with anything that works for you.

Related

PHP Soap - Authentication header missing

I have been using an API for a WMS which has updated to include authentication headers. I have been provided some required details but have been unable to sucessfully use the API. I have asked the developers but they are unable to help as they do not use PHP.
Previous to the last update, this would work:
$wsdl = URL_HERE;
$soapClient = new SoapClient($wsdl);
$params = array('customer' => $get_users_company->custcode_code);
$response = $soapClient->GetProducts($params);
With the authentification headers, this is what I currently have which is causing the error Authentication header missing
$wsdl = URL_HERE;
$ns = NAMESPACE_HERE;
$soapClient = new SoapClient($wsdl);
$headerbody = array('ID' => 'PROVIDED_ID_HERE', 'KEY' => 'PROVIDED_KEY_HERE');
$headers = new SOAPHeader($ns, 'AuthHeader', $headerbody);
$soapClient->__setSoapHeaders($headers);
$response = $soapClient->__soapCall("GetProducts", array('customer' => $get_users_company->custcode_code));
I'm not 100% sure I am doing this correctly, but without the last line, I get no errors and the page loads fine (No results). Am I correct in thinking the headers are being sent?
I have heard the good old, "we can't help because we are XML Gods and your little php is beneath us"...but you can still get technical support from them by speaking their XML language. Dump out your actual, raw XML and communicate with them using that - don't mention PHP.
Follow the example here and get your request. Make sure it is matching what the documentation of your API is requesting. If it is, call your technical support and show them your XML. If it isn't, then, you know what you need to fix.
When using $soapClient->__soapCall() the second parameter takes an array, and your data structure is also an array, so you maybe should be doing:
$params = array('customer' => $get_users_company->custcode_code);
$response = $soapClient->__soapCall("GetProducts", array($params));
Or just leave it as:
$response = $soapClient->GetProducts($params);
Which looks nicer.

Twitter Oauth Statuses/show in codeigniter returns error

I am using this library for implementing twitter Oauth in my project,
and codeigniter if that is relevant in this case.
I am trying to get a tweet based on the tweet id, but it's returning an error:
{"errors":[{"message":"Sorry, that page does not exist","code":34}]}
Here is the code I am using:
public function gettweetbyid() {
$this->config->load('twitter');
$consumer = $this->config->item('twitter_consumer_token');
$consumer_secret = $this->config->item('twitter_consumer_secret');
$access_token = $this->config->item('twitter_access_token');
$access_token_secret = $this->config->item('twitter_access_secret');
$connection = $this->twitteroauth->create($consumer, $consumer_secret, $access_token, $access_token_secret);
$content = $connection->get('account/verify_credentials');
$data = array(
'id' => $this->input->get('id')
);
$tweets = $this->twitteroauth->get('statuses/show', $data);
echo json_encode($tweets);
}
I know everything is right because I am using the plugin as well to get tweets based on a hashtag, and this is no problem at all.
The URl I am getting looks very similar to their docs url:
https://api.twitter.com/1.1/statuses/show.json?id=408541017676460000&....
I'm looking at this for a whole day now and my time is running out...
Any help would be appreciated!
Edit: Also, is there any way on twitter I can check if the ID actually exists? Altho I don't think that is the problem, I'd like to know for future reference
For those people stumbling on this:
I get the id from a previous request to twitter, and I found out you have to use the id_str from the json data twitter returns, and not the normal id. the string id and normal id are different. God may know why, but I'm just glad this got solved!

Facebook PHP API not always returning event venue data

I have 3 events:
https://www.facebook.com/491594114271958 (Mad Haus)
https://www.facebook.com/569226999799343 (Deuglish)
539504962802119/ (Piffle)
All are being fetched via the PHP
$config = array();
$config['appId'] = $appId;
$config['secret'] = $secret;
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
$ev = $facebook->api('/'.$id."?fields=cover,description,location,name,owner,venue",'GET');
print_r($ev);
For some reason Mad Haus and Piffle do not return venue data but Deuglish does. All events return basic data such as title, description and start time. When I run them through the Graph API explorer the venue data is returned as expected but not through the PHP API, any ideas? I can not for the life of me see the difference with these 3 events.
Thanks,
Caroline
Well, if you don't want to wait for the Facebook developers to fix the bug, you can try the same by making a simple GET request (of course along with an Access Token) to Graph API using PHP cURL. Since you are able to retrieve the venues while using the Graph API explorer, I'm pretty sure you'll be able to get them by making a simple GET request using PHP cURL.
This however involves an overhead of parsing the received JSON object and dealing with errors will be a little difficult here. But you can give it a shot.

Rendering SoundCloud widget for a private track using PHP API

I am trying to render a SoundCloud HTML5 widget using the PHP API, but every time I run the command I think should return the HTML for the widget, I simply get an Exception:
The requested URL responded with HTTP code 302
I realise this is a redirect. What I don't know is why that's all I ever get, or what to do about it to actually get the widget HTML.
The documentation on the API says that to embed the widget using PHP you should do this:
<?php
require_once 'Services/Soundcloud.php';
// create a client object with your app credentials
$client = new Services_Soundcloud('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET');
// get a tracks oembed data
$track_url = 'http://soundcloud.com/forss/flickermood';
$embed_info = $client->get('/oembed', array('url' => $track_url));
// render the html for the player widget
print $embed_info['html'];
I'm running this:
// NB: Fully authorised SoundCloud API instance all working prior to this line
// $this->api refers to an authorised instance of Services_Soundcloud
try {
$widget = array_pop(
json_decode( $this->api->get('oembed', array('url' => $track_url)) )
);
print_r($widget);
} catch (Exception $e)
{
print_r($e->getMessage());
}
where "track_url" is actually the URL I get back when asking SoundCloud for a track object earlier in the app using the same API.
I'm not actually sure this URL is correct in the first place, because the track object I get back gives the 'uri' in the form:
[uri] => https://api.soundcloud.com/tracks/62556508
The documentation examples all have a straight http://soundcloud.com/username/track-permalink URL - but even using a known path to a public track the attempt to run the API oembed method fails... I still get a 302 Exception.
Finally, there are mentions of setting "allow_redirects" to false in the 'get' command, but this has no effect when I add to the parameters used to build the query to the API. I also tried adding additional cURL options, but that too had no effect.
I have definitely enabled API access to the track within SoundCloud.
Kind of banging my head off the wall on this. If anyone has any pointers I'd be very grateful to hear them. Just for clarity's sake, I am able to access all the user data, comments etc. via the API instance I have created, so it appears to be working fine.
Thanks for pointing this out. There was a bug in the documentation that lead you astray. Sorry about that. I've updated the docs to fix the bug. Here's the updated code sample:
<?php
require_once 'Services/Soundcloud.php';
// create a client object with your app credentials
$client = new Services_Soundcloud('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET');
$client->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1));
// get a tracks oembed data
$track_url = 'http://soundcloud.com/forss/flickermood';
$embed_info = json_decode($client->get('oembed', array('url' => $track_url)));
// render the html for the player widget
print $embed_info->html;
Note the differences:
You need to set CURLOPT_FOLLOWLOCATION to 1 as mentioned in the comments above.
You need to wrap the return from $client->get in json_decode
The result is an stdClass object, not an Array and so the html property has to be accessed using the -> operator.
Hope that helps. Feel free to comment in case you're still having problems and I'll amend my answer.

user_timeline twitter API problem

I'm using http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses-user_timeline to read the tweets of the user. When I used basic auth, it worked fine. When I switched to OAuth, the 'page' parameter stopped working.
Like so:
http://api.twitter.com/1/statuses/user_timeline/16.xml?count=25&page=2
When I use OAuth to fetch the request, it always returns the first page. I check my code. I even echoed the exact same line, and it was exactly what I needed. The XML is exactly what I want, but when I use OAuth to fetch the XML, it returns the wrong XML.
I use abraham's php library.
So basically. The XML is correct and when entered as URL it returns the correct XML, but when trying to fetch it through OAuth, it returns the wrong XML.
Any clue?
In the latest version of my library (currently 0.2.0-beta2) it should be called like this:
$to->format = 'xml';
$content = $to->get('statuses/user_timeline/16', array('count' => 25, 'page' => 2));
I just encountered the same problem - when i am using oauth, Twitter seems to be oblivious about GET parameters. I am using php lib form here: http://abrah.am
this doesnt work:
$url =
"http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=$name&count=199&page=1";
$content = $to->OAuthRequest($url);
but this does:
$url = "http://api.twitter.com/1/statuses/user_timeline.rss";
$content = $to->OAuthRequest($url, array('screen_name' => $name,'count' => 199, 'page' =>1), 'GET');

Categories