Zend Gdata Library - php

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));

Related

Adsense API get single domain

I'm currently looking into selecting a single domain on the Adsense API to see if the domain is currently available in Adsense. Selecting all
domains goes well, but it seems a bit cumbersome to loop all properties. So I'm looking for a single select
Current code:
# Setup client
$client = new Google_Client();
$client->setAccessToken(GOOGLE_ACCESS_TOKEN);
# Setup api
$adsense = new \Google\Service\Adsense($client);
# Set Scope to adsense
$client->setScopes(['https://www.googleapis.com/auth/adsense']);
# list account sites
$ads = $adsense->accounts_sites->listAccountsSites("accounts/pub-xxxxxxxxxxxxxxxx");
#loop through the domains and check if in array
foreach ($ads as $a) {
$ad_sites[] = $a['domain'];
}
# Result:
$in_adsense = in_array($domain, $ad_sites)
What looks promising is the current doc by Google:
https://developers.google.com/adsense/management/reference/rest/v2/accounts.sites/get
And the corresponding get method: https://developers.google.com/adsense/management/reference/rest/v2/accounts.sites/get
I however can't seem to find the right syntax to select a single domain on the Google SDK. Any help would be appriciated.
Turns out I've overlooked a possibility to use a gRPC query. This turns out to be the following code, based on the example created by #Wahyu Kristianto.
$domain = "google.com";
$adsense = new \Google\Service\Adsense($client);
try {
$site = $adsense->accounts_sites->get("accounts/pub-xxxxxxxx/sites/{$domain}");
$in_adsense = true;
} catch (\Exception $e) {
$in_adsense = false;
}
So we're calling a gRPC query to the get method: accounts/pub-xxxxxxxx/sites/google.com

PHP Posting Images to Twitter with Link to Webpage?

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

How to create bitly shortened url from user's inputed text?

Begginer here, people. Could anybody suggest any kind of solution? I've an user inputed text.
First of all I check if the text has any urls:
$post = preg_replace('/https?:\/\/[\w\-\.!~?&+\*\'"(),\/]+/','<a class="post_link"
href="$0">$0</a>',$post);
And after that I need to retrieve that url and put as a variable($url) to this function:
$short=make_bitly_url('$url','o_6sgltp5sq4as','R_f5212f1asdads1cee780eed00d2f1bd2fd794f','xml');
And finally, echo both url and user's text. Thanks in advance for ideas and critiques.
I've tried something like that:
$post = preg_replace('/https?:\/\/[\w\-\.!~?&+\*\'"(),\/]+/e',$url,$post){
$shorten = make_bitly_url($url,'o_6sgltpmm5sq4','R_f5212f11cee780ekked00d2f1bd2fd794f','json');
return '<a class="post_link" href="$shorten">$shorten</a>';
};
But even for me it looks some kind of nonsense.
Bitly does have an API available for use. You should check out API Documentation
Here's how to use the bit.ly API from PHP:
/* make a URL small */
function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1')
{
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
//get the url
//could also use cURL here
$response = file_get_contents($bitly);
//parse depending on desired format
if(strtolower($format) == 'json')
{
$json = #json_decode($response,true);
return $json['results'][$url]['shortUrl'];
}
else //xml
{
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
/* usage */
$short = make_bitly_url('http://davidwalsh.name','davidwalshblog','R_96acc320c5c423e4f5192e006ff24980','json');
echo 'The short URL is: '.$short;
// returns: http://bit.ly/11Owun
Source: David Walsh article
HOWEVER, if you wanted to create your own URL shortening system (similar to bit.ly -- and surprisingly easy to do), here is an 8-part tutorial from PHPacademy on how to do that:
Difficulty level: beginner / intermediate
Each video is approx ten minutes.
Part 1
Part 2
Part 3
Part 4
Part 5
Part 6
Part 7
Part 8

How to get results from "Elastica_ResultSet" object

I am using the "elastica" php client for ElasticSearch.
I'm a bit new to OO-programming, especially in php.
However, I have managed to search my elasticsearch server using the elastica php client and store the response in an "Elastica_ResultSet" object. I have had no luck accessing the contents of that object whatsoever.
I would like to be able to list the total number of results, find an elasticsearch record id of a result and get the full content of an elasticsearch record for that result.
The Elastica class reference can be found here http://ruflin.github.com/Elastica/api/index.html , although I don't know what to do with it.
Here is the php code I have been using to get this far:
<?php
function __autoload_elastica ($class) {
$path = str_replace('_', '/', $class);
if (file_exists('extentions/' . $path . '.php')) {
require_once('extentions/' . $path . '.php');
//echo "$path EXISTS!!!";
}
}
spl_autoload_register('__autoload_elastica');
// New ES Client
$client = new Elastica_Client();
// Set Index
$index = $client->getIndex('test1');
// Set Document Type
$type = $index->getType('user');
// Perform Search
$resultSet = $index->search('halo');
?>
So basicaly you can use var_export to output your resultset
But in general the elastica search returns a Elastica_ResultSet object which has several attributes you can use like count, totalHits facets and so on.
and also holds an array of Elastica_Result objects these can be accessed either by calling the Elastica_ResultSet getResults() method or by using the current() and next() methods or by simply using the php foreach function
The Elastica_Result the data of the results and also has several methods you can use.
getId(), getVersion(), getData() and so on.
// Set Document Type
$type = $index->getType('user');
// Perform Search
$resultSet = $index->search('halo');
// Get IDs
$resultIDs = array();
foreach($resultSet as $result){
$resultIDs[] = $result->getId();
}
I would like to let you know something that was a bit hard for me to get.
The query and the sorting of results
// Set the query terms for your search
$queryTerm = new Elastica_Query_Terms();
$queryTerm->setTerms('user', array("test", "test1"));
// Create the sorting array
$sort = array("user" => array("order" => "desc"));
// Create the query
$query = Elastica_Query::create($queryTerm);
// Set the sorting to the query
$query->setSort($sort);
// Perform the search
$resultSet = $index->search($query);
Hope this helps
After a couple of months OO practise, it seemed performing a simple var_dump($resultSet) would have provided me with the structure and contents of the returned object... can't believe that nobody made any suggestions for such a basic question ;)

PHP - Codeigniter twitter loops

new here!!
Im trying to get tweets to display on a site(framework is codeigniter). I am using the twitter api (for example: https://api.twitter.com/1/statuses/user_timeline/ddarrko.xml) to get the tweets and subsequently insert them into a database which I will then get to display on the site. The actual code runs fine the issue is it only ever processes one tweet. my code is -
//get twitter address
$this->load->model('admin_model');
$getadd = $this->admin_model->get_settings("twitter_address");
$twitter_user = $getadd->item_value;
//define twitter xml file
$xmlpath = "https://api.twitter.com/1/statuses/user_timeline/".$twitter_user.".xml";
$xml = simplexml_load_file($xmlpath);
foreach ($xml->status as $tweet);
{
echo "<pre>";print_r($xml);echo "</pre>";
$this->data->username=$twitter_user;
$this->data->twitter_status=$tweet->text;
$this->data->pub_date=$tweet->created_at;
//load model and insert tweets;
$this->load->model('tweet_model');
$this->tweet_model->insert_tweets($this->data);
}
as you can see I am defining to run each status in xml file. The echo pre line is me testing because when printing $tweet only one tweet is coming up however even if i loop through just $xml still only one tweet is processed despite there being loads in the file.
any help/advice would be greatly appreciated!
Below code will give you all the available tweets from XML. you can manipulate below logic as per your need.
$xmlpath = "https://api.twitter.com/1/statuses/user_timeline/".$twitter_user.".xml";
$xml = simplexml_load_file($xmlpath);
$count_tweet = sizeof($xml);
for($i=0 ; $i < $count_tweet ; $i++)
{
echo"<br>Tweet: ". $xml->status[$i]->text;
echo"<br>date:".$xml->status[$i]->created_at."<br>";
}
Thanks.

Categories