PHP Posting Images to Twitter with Link to Webpage? - php

I'm having a hard time using the "link shortening" service in the twitter API
https://dev.twitter.com/docs/tco-link-wrapper/faq
I'm under the impression that a standard type of link should automatically become shortened and clickable when it passes through the API. Yet regardless of how I send the link through, its always as non-clickable text...never shortened, and always counting against the 140 limit.
When using the 1.1 api, how do we direct twitter to deal with URLs?
//this here is simply binary stuff uploaded and posted, using twitteroath library
$media = ss_get_image_binary($file_attachment_path);
$params = array(
'media[]' => "{$media};type=image/jpeg;filename={$file_attachment_path}",
);
$resource_url = 'https://api.twitter.com/1.1/statuses/update_with_media.json';
the link part:
//I'm making my own hashtags, and counting post length stuff myself...
//the backlink variable here is just a typical http: link
$hashtags = ss_make_hastags($tags, $id);
$title = ss_twitter_status_format($image->post_title, $hashtags);
$backlink = get_permalink($id);
$params['status'] = $backlink . ' ' . $hashtags;
//lastly, everything is posted to twitter
$connection = ss_twitter_communication_setup();
$tweet = $connection->post($resource_url, $params);
The image and status are posting fine. The links never become "active" or clickable. And they count against the 140 limit.
one more thing --
This is the class that everything leads back to, setting up communication
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);

Found the problem:
LOCALHOST does not work.
facepalm

Related

Wikipedia search api with much side information

I want to take some information about my query from wikipedia using php. I want to have a description, title, img url (if exists), and some side information about it. For example like the google wikipedia information, with much side information.
Until now i have only the description & title.
My code:
$q = str_replace("#", "", ucwords($query));
$opts = array('http' =>
array(
'user_agent' => 'Mozilla Firefox www (http://www.mozilla.org)'
)
);
$context = stream_context_create($opts);
$url = 'en.wikipedia.org/w/api.php?action=opensearch&search='.$q.'&limit=1&namespace=0&format=xml';
#$wiki_arr = simplexml_load_file($url);
if(#$wiki_arr->Section->Item->Text == "" or #strtolower($wiki_arr->Section->Item->Text) != strtolower($q))
{
return "";
}
$title = $wiki_arr->Section->Item->Text;
$description = $wiki_arr->Section->Item->Description;
$url = $wiki_arr->Section->Item->Url;
$result = "".$title.", ".$description.", ".$url."";
Once again, there is no such thing as a Wikipedia API. There is a MediaWiki web API. If something doesn't exist there, it doesn't exist at all in wikipedia.org.
For your question: you said you want to do something like the Wikipedia card in the Google Knowledge Graph. What powers that? Freebase. What's the Wikimedia projects equivalent of Freebase? Wikidata.org. (And not only for Wikipedia, but also for Wikisource etc.) So, please use the Wikidata data, via its API which is documented by itself: http://wikidata.org/w/api.php
You can check the [[wikidata:list of properties]] to know what kind of data to access, and [[mediawikiwiki:Category:Wikibase]] for other libraries to access the API. An example of what you can do this way is wdsearch.js, another reasonator at tools.wmflabs.org (sorry, can't add more links).

How can I make this AJAX call work if you need to be authenticated?

I would like to know how I can make AJAX calls to the Twitter API in order to get pictures of twitter users and display them back to the browser . The thing is that when I use the PHP method , it takes a VERY VERY LONG time to display profile pictures of a 100 twitter users generated by a loop .. So through this URL below that returns information about a twitter user, how could I use it with Jquery AJAX in order to fetch profile pictures images ? This requires the call to be authenticated ..
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2
Below is the PHP method I used , but it's SOO SLOW ..
$id = $_GET['screen_name'];
* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
foreach($id as $key => $value) {
$url = $connection->get('users/show', array('screen_name' => $value));
$results = json_encode($url);
$data = json_decode($results, true);
$image = '';
if(is_array($data)){
$image = $data['profile_image_url']; ;
$image_bigger = str_replace('_normal', '_bigger',$image);
}
}
?>
Try the users/lookup endpoint which will let you pull 100 profiles at a time.
https://dev.twitter.com/docs/api/1.1/get/users/lookup
Note also that you cannot make pure ajax calls directly to the new Twitter API without exposing your secret keys to the public. You'll have to proxy these calls via your own back end.

eBay API - FindItemsAdvanced call in PHP does not return any response

Im trying to perform a simple call using ebays search API, when I make a call I get no response, and the problem is with the actual call itself.
$endpoint = 'http://open.api.ebay.com/shopping?';
$responseEncoding = 'XML';
$version = '631'; // API version number
$appID = 'asdf3e6e3';
$itemType = "AllItemTypes";
$itemSort = "EndTime";
//find items advanced
$apicalla = "$endpoint"
."callname=FindItemsAdvanced"
."&version=$version"
."&siteid=0"
."&appid=$appID"
."&MaxEntries=10"
."&ItemSort=EndTime"
."&ItemType=AllItemTypes"
."&IncludeSelector=SearchDetails"
."&responseencoding=$responseEncoding";
$resp = simplexml_load_file($apicalla);
this call is the equivalent to
http://open.api.ebay.com/shopping?callname=FindItemsAdvanced&version=631&siteid=0&appid=asdf3e6e3&MaxEntries=10&ItemSort=EndTime&ItemType=AllItemTypes&IncludeSelector=SearchDetails&responseencoding=XML
My question is what am I missing to make this simple search call?
It looks like you're trying to use eBay's Shopping API, specifically the FindItemsAdvanced call which I believe was deprecated quite some time ago and may no longer be functional (I no longer see it in the call reference). What you want to do is use use findItemsAdvanced from eBay's Finding API.
First, you'll need to change your API endpoint & query string parameters a bit (see the aforementioned findItemsAdvanced call reference for the specifics, but I believe it'll look more like this (I haven't touched my findItemsAdvanced calls in at least 6 months, so I haven't tested this):
$endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1?';
$responseEncoding = 'XML';
$version = '1.8.0'; // API version number (they're actually up to 1.11.0 at this point
$appID = 'asdf3e6e3';
$itemSort = "EndTimeSoonest";
//find items advanced
$apicalla = "$endpoint"
."OPERATION-NAME=findItemsAdvanced"
."&SERVICE-VERSION=$version"
."&GLOBAL-ID=EBAY-US"
."&SECURITY-APPNAME=$appID"
//."&MaxEntries=10" // look for an equivalent for this (maybe paginationInput.entriesPerPage?)
."&sortOrder=EndTimeSoonest"
//."&ItemType=AllItemTypes" // not needed AFAICT, otherwise look at itemFilterType
."&descriptionSearch=true";
."& RESPONSE-DATA-FORMAT=$responseEncoding";
$resp = simplexml_load_file($apicalla);
In addition to this, to use findItemsAdvanced, you must specify what you're searching for either by category (categoryId) or by keywords (keywords), hence the "Please specify a query!" error message.
So, you also need to add something like the following (assuming keywords):
$keywords = "something";
$apicalla .= "&keywords=" . urlencode($keywords);
Giving you the following:
$endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1?';
$responseEncoding = 'XML';
$version = '1.8.0'; // API version number (they're actually up to 1.11.0 at this point
$appID = 'asdf3e6e3';
$itemSort = "EndTimeSoonest";
$keywords = "something"; // make sure this is a valid keyword or keywords
//find items advanced
$apicalla = "$endpoint"
."OPERATION-NAME=findItemsAdvanced"
."&SERVICE-VERSION=$version"
."&GLOBAL-ID=EBAY-US"
."&SECURITY-APPNAME=$appID"
//."&MaxEntries=10" // look for an equivalent for this (maybe paginationInput.entriesPerPage?)
."&sortOrder=$itemSort"
//."&ItemType=AllItemTypes" // not needed AFAICT, otherwise look at itemFilterType
."&descriptionSearch=true";
."& RESPONSE-DATA-FORMAT=$responseEncoding"
."&keywords=" . urlencode($keywords);
$resp = simplexml_load_file($apicalla);
One final note: If you want to load further details of specific items that you find in your results, you'll still want to use the Shopping API (specifically the GetSingleItem & GetMultipleItems calls). So, you may ultimately use a mix of the Shopping & Finding APIs.
It should be something like:
<?php
$url = 'http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsAdvanced&SERVICE-VERSION=1.11.0&SECURITY-APPNAME=YOUR_APP_ID&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD&paginationInput.entriesPerPage=2&keywords=ipod&siteid=203&GLOBAL-ID=EBAY-IN';
$xml = file_get_contents( $url );
$xml = simplexml_load_string( $url );
?>
Log-in to your ebay developer account and click on this link: Test your calls with API Test Tool
Hope this helps.

Youtube PHP API: Verify a youtube username matches up with authentication token

I have a web application where I'm having the user log into youtube to authorize my web application to access their account. However, I need their youtube user name to actually list their uploaded videos, and I want to ensure that the youtube username they provide matches up with the account they used to authorize with. In other words, I only want the user to be able to share videos that they've uploaded, and not someone else's. Is there a way to do this?
I had done this before with C# .Net with the following code:
YouTubeRequestSettings yt_settings = new YouTubeRequestSettings(<devID name>, devKey, auth);
YouTubeRequest yt_request = new YouTubeRequest(yt_settings);
Uri uri = new Uri("http://gdata.youtube.com/feeds/api/users/default/uploads/?start-index=1&max-results=1");
Feed<Video> videoFeed = yt_request.Get<Video>(uri);
string uploader = "";
if (videoFeed.Entries.Count() > 0)
uploader = videoFeed.Entries.ElementAt(0).Uploader;
But when I try something similar with php I get what appears to be a standard feed:
$yt = new Zend_Gdata_YouTube($http_client,<devID name>,null,$_yt_dev_key);
$feed_url = urlencode("http://gdata.youtube.com/feeds/api/users/default/uploads/?start-index=1&max-results=1");
$videoFeed = $yt->getVideoFeed();
if(count($videoFeed) > 0)
{
$videoEntry = $videoFeed[0];
echo var_dump($videoEntry);
}
Anyone have any idea if I'm doing something wrong?
------------------ UPDATE ----------------------
I have the solution to getting youtube video feed for the user in the authenticated session. $videoFeed = $yt->getuserFeed("default");
Though, I'm still looking at how to get the uploader name from this so that I can perform further video listings directly from javascript (like I had done with my old C#/Asp .Net web app).
----------- A RATHER ROUGH SOLUTION ------------
Well, this isn't exactly an elegant solution, but it's what I have working.
The following will extract the youtube username from a VideoEntry object...
$videoFeed = $yt->getuserUploads("default");
if(count($videoFeed) > 0)
{
$videoEntry = $videoFeed[0];
$v_dump = var_export($videoEntry, true);
$check_for = "http://gdata.youtube.com/feeds/api/users/";
$pos = strpos($v_dump,$check_for);
$start_pos = $pos + strlen($check_for);
$user_name = "";
for($i = $start_pos; $i < strlen($v_dump); $i++)
{
if(($v_dump[$i] == '/') or ($v_dump[$i] == '?'))
break;
$user_name .= $v_dump[$i];
}
echo $user_name;
}
Basically, I'm parsing through the entire video entry variable string representation for the first generic part of the feed url, then getting the next token in the url which is the username. In other words, I'm locating this url in the var dump and parsing the username out:
http://gdata.youtube.com/feeds/api/users/<username>/uploads
If anyone can find a better and cleaner way to do it, that'd be awesome. Parsing a large string like this seems to be a very dirty way of doing this.
See http://code.google.com/apis/youtube/2.0/developers_guide_php.html#Enabling_user_interaction
It says,
Note: Using the string default instead of a username retrieves the profile of the currently authenticated user.
So you can get all the user data about the currently authenticated user using this.
Hope it helps!
Try this:
$user = $yt->getUserProfile('default');
$username = $user->getUserName();

Zend Gdata Library

hi i am useing zend Gdata Library for youtube videos i am trying to show more then 20 videos or i can set how much videos i want to show but i found no options
$yt = new Zend_Gdata_YouTube();
$videoFeed = $yt->getUserFavorites('liz');
is there a way to get more then 20 videos or less Zend Gdata default is 20
you can view here
http://framework.zend.com/manual/en/zend.gdata.youtube.html
After some more research I think I found some solution.
Check there http://code.google.com/apis/youtube/2.0/developers_guide_php.html#Pagination
You should write recursive function loop trough video feed pages. For my app it was something like this (method in class):
<?php
//...
protected function build_favs_html($videos) {
//just saving html here. Mind the .= operator.
// I think you'll be doing this in some other way
$this->_html_response .= View::factory('videos')
->set('videos', $videos)
->set('type', 'load_favs');
// See whether we have another set of results
try {
$videos = $videos->getNextFeed();
}
catch (Zend_Gdata_App_Exception $e) {
//break function execution if there are no more result sets
return null;
}
//if there are result sets we continue calling same function on and on
$this->build_favs_html($videos);
}
I'm not sure if this can be done when requesting user favorites, as I've never used that feature, but when requesting videos via search terms you can set the results number via the setMaxResults method. You might be able to work this in with your user favorites request.
Here's a snippet of code we use:
$yt = new Zend_Gdata_YouTube();
$yt->setMajorProtocolVersion(2);
$query = $yt->newVideoQuery();
$query->setOrderBy('viewCount');
$query->setSafeSearch('strict');
$query->setFormat($searchFormat);
$query->setVideoQuery($searchTerms);
$query->setMaxResults($limit); // number of returned results set here
$query->setStartIndex($offset);
$results = $yt->getVideoFeed($query->getQueryUrl(2));

Categories