How to read twitter feed

HTML Code

<ul class=”twitter_post”>
<div id=”twitter-feed”></div>
</ul>

 

twitter-feed-reader.js

$(document).ready(function() {

loadLatestTweet();

});

//Twitter Parsers

String.prototype.parseURL = function() {

return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&~\?\/.=]+/g, function(url) {

return url.link(url);

});

};

String.prototype.parseUsername = function() {

return this.replace(/[@]+[A-Za-z0-9-_]+/g, function(u) {

var username = u.replace(“@”,””)

return u.link(“http://twitter.com/”+username);

});

};

String.prototype.parseHashtag = function() {

return this.replace(/[#]+[A-Za-z0-9-_]+/g, function(t) {

var tag = t.replace(“#”,”%23″)

return t.link(“http://search.twitter.com/search?q=”+tag);

});

};

function parseDate(str) {

var v=str.split(‘ ‘);

return new Date(Date.parse(v[1]+” “+v[2]+”, “+v[5]+” “+v[3]+” UTC”));

}

function loadLatestTweet(){

var numTweets = 1;

var _url = ‘https://api.twitter.com/1/statuses/user_timeline/TweetID.json?callback=?&count=’+numTweets+’&include_rts=1&#8242;;

$.getJSON(_url,function(data){

for(var i = 0; i< data.length; i++){

var tweet = data[i].text;

var created = parseDate(data[i].created_at);

var createdDate = created.getDate()+’-‘+(created.getMonth()+1)+’-‘+created.getFullYear()+’ at ‘+created.getHours()+’:’+created.getMinutes();

tweet = tweet.parseURL().parseUsername().parseHashtag();

tweet += ‘

$(“#twitter-feed”).append(‘

‘+tweet+’

‘);

}

});

}

Advertisements

Connect with mysql database using PDO class

protected $link;
protected $db_host = ‘localhost’;
protected $db_name = ‘blog’;
protected $db_user = ‘root’;
protected $db_pass = ”;

function __construct() {

try {
$this->link = new PDO(“mysql:host=$this->db_host;dbname=$this->db_name”, $this->db_user, $this->db_pass);
return $this->link;
} catch (PDOException $e) {
echo $e->getMessage();
}
}

Change the maximum upload size with PHP

Change the maximum upload size with PHP

Depending on how you host your website (or application) there are different ways to change the PHP settings. In particular, the most asked about problem, changing the maximum upload size.

There are 3 settings PHP uses that limit your uploading ability:

  • post_max_size
    This is the combined maximum size of all files sent on the form. If you have 2 file fields on your form, the total filesize of the 2 files must not exceed the post_max_size value.
  • upload_max_filesize
    This is the filesize limit of each individual file.
  • memory_limit
    PHP scripts have a memory limit, and generally speaking this can prevent some uploads from working. The limit should be set at a reasonable level, of course you won’t need 20mb for a simple ‘hello world’ script. Try slowly increasing this value if you find that uploads still aren’t working.

If you host your site remotely, you should check their documentation on how to change the PHP configuration settings. But if you’re not so inclined, and prefer a trial and error approach, here’s a summary of things to try:

Change php.ini directly

If you host your site on a server that you have access to, you can change your php.ini file directly. This is the easiest approach. Your php.ini file should exist in the PHP installation directory. Open it in your favorite text editor and search for these lines and change them:

memory_limit = 8M
post_max_size = 8M
upload_max_filesize = 2M

You may need to restart apache for the changes to take effect.

Changes to .htaccess

.htaccess files only apply to Apache webservers. They are files that append and change certain values that Apache uses and they are placed in the root folder (and all folders beneath will use those settings). If Apache uses PHP as a Module, then you can add values to the .htaccess file:

php_flag file_uploads On
php_value memory_limit 8M
php_value post_max_size 8M
php_value upload_max_filesize 2M

If you try this approach, and your webserver displays a 500 internal server error, then PHP is not running as a module and Apache didn’t like the php_value entries. You will need to remove your changes.

Upload a php.ini file

This generally works if the changes to .htaccess method doesn’t. Some web hosts that use apache, also use PHP as a CGI. Then also, most times, this allows changes to the PHP configuration using a custom php.ini file that you can upload. You don’t want to include every setting available to PHP in your file. Only include the ones you want to change, and don’t forget the heading [PHP]:

[PHP]
; Whether to allow HTTP file uploads.
file_uploads = On
; Maximum amount of memory a script may consume (8MB)
memory_limit = 8M
; Maximum size of POST data that PHP will accept.
post_max_size = 8M
; Maximum allowed size for uploaded files.
upload_max_filesize = 2M

Still not working

In the event these techniques didn’t work for you, check with your host’s documentation.

Read Youtube JSON Feed Using PHP

<!doctype html>
<html>
<head>
<meta charset=”utf-8″>
<title>Latest Youtube Videos</title>
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js”></script&gt;
<script src=”get-latest-videos.js”></script>

</head>

<body>
<div id=”videogroup”> <!– here will be loaded your videos –> </div>
</body>
</html>

get-latest-videos.js

// JavaScript Document
function show_my_videos(data){
html = [‘<ul id=”youtube-videos”>’];
$(data.feed.entry).each(function(entry){

entriesTitle=this.title.$t;
url = this.link[0].href;
url_thumbnail = this.media$group.media$thumbnail[3].url;
description = this.media$group.media$description.$t;

html.push(‘<h4>’+entriesTitle+'</h4>’);
html.push(‘<li><a href=”‘+url+'” target=”_blank”>’);
html.push(‘<img src=”‘+url_thumbnail+'” alt=”‘+description+'”><br />’);
html.push(‘</a></li>’);
html.push(”+description+”);
html.push(‘<br /><br />’);
});
html.push(‘</ul>’);
$(“#videogroup”).html(html.join(”));
}

$.ajax({
type: “GET”,
url: “http://gdata.youtube.com/feeds/users/s4sanjoy/uploads?alt=json-in-script&max-results=4&format=5&#8221;,
cache: false,
dataType:’jsonp’,
success: function(data){
show_my_videos(data);
//If you want to see in console…
// console.log(data);
// console.log(data.feed.entry);
// $(data.feed.entry).each(function(){
// console.log(this.link[0].href);
// console.log(this.media$group.media$thumbnail[3].url);
// console.log(this.media$group.media$description.$t);
// });
}
});

 

Read Youtube Feed XML Using PHP

<!doctype html>
<html>
<head>
<meta charset=”utf-8″>
<title>Untitled Document</title>
<script src=”http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js&#8221; type=”text/javascript”></script>
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js&#8221; type=”text/javascript”></script>
<script type=”text/javascript”>
function play(clip){
var mv = “http://www.youtube.com/v/”+clip+”&autoplay=0&rel=0&#8221;;
swfobject.embedSWF(mv, “myContent”, “375”, “295”, “8”);
}
$(document).ready(function() {
play(‘CsGYh8AacgY’);
});
</script>
<style type=”text/css”>
.thumb{
height:150px;
width:150px;
}
</style>
</head>

<body>
<div id=”myContent”></div>
<br />

<B>Choose Video:</B>
<br/>
<?php
//SETTINGS
$channel_name = ‘s4sanjoy’; //Be sure to change this to your channel
$count = 6; //# of videos you want to show (MAX = 20)
$em_width = 420; //width of embedded player
$em_height = 315; //height of embedded player
$wrap_class = ‘video’; //class name for the div wrapper
//The output…
$sxml = simplexml_load_file(“http://gdata.youtube.com/feeds/api/users/$channel_name/uploads?max-results=$count&#8221;);

foreach ($sxml->entry as $entry) {
$vidKey = substr(strrchr($entry->id, ‘/’), 1);
$title = $entry->title;

// get nodes in media: namespace for media information
$media = $entry->children(‘http://search.yahoo.com/mrss/&#8217;);

// get video player URL
$attrs = $media->group->player->attributes();
$watch = $attrs[‘url’];

// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$thumbnail = $attrs[‘url’];

//echo “<a href=# onclick = play(“.$vidKey.”); return false>”.$title.”</a><br/>”;
?>
<img class=”thumb” src=”<?php echo $thumbnail ;?>”>
<br/>
<a href=”#” onclick=”play(‘<?php echo $vidKey ;?>’); return false” style=”color:#9b9898; font-size:12″><?php echo $title ;?></a>
<br/>
<?php
}
?>

</body>
</html>

On Click Play Youtube Video

<!doctype html>
<html>
<head>
<meta charset=”utf-8″>
<title>Untitled Document</title>
<script src=”http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js&#8221; type=”text/javascript”></script>
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js&#8221; type=”text/javascript”></script>
<script type=”text/javascript”>
function play(clip){
var mv = “http://www.youtube.com/v/”+clip+”&autoplay=0&rel=0&#8221;;
swfobject.embedSWF(mv, “myContent”, “375”, “295”, “8”);
}
$(document).ready(function() {
play(‘CsGYh8AacgY’);
});
</script>
</head>

<body>
<div id=”myContent”></div>
<br />

<B>Choose Video:</B>
<a href=”#” onclick=”play(‘yFhEsUUvwYE’); return false” style=”color:#9b9898; font-size:12″>Charlie 1</a> |
<a href=”#” onclick=”play(‘596gXXYcMIY’); return false” style=”color:#9b9898; font-size:12″>Charlie 2</a> |
<a href=”#” onclick=”play(‘wOVAF68uvM8’); return false” style=”color:#9b9898; font-size:12″>Charlie 3</a>

</body>
</html>

Read image link and save to computer using php

<?php
ini_set(‘max_execution_time’, 180);
function GetImageFromUrl($link)

{

$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, 0);

curl_setopt($ch,CURLOPT_URL,$link);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result=curl_exec($ch);

curl_close($ch);

return $result;

}

$url=$_POST[‘txtUrl’];

$html = file_get_contents($url);

$doc = new DOMDocument();
@$doc->loadHTML($html);

$tags = $doc->getElementsByTagName(‘img’);

foreach ($tags as $tag) {

$sanjoy= $tag->getAttribute(‘jqimg’);

if (substr($sanjoy, 0, 5) == ‘/pic/’) {
//echo $abid ;

$ImgName=substr($abid, 5);
$Full_Image_Path=’http://www.pleaserusa.com/pic/&#8217; . $ImgName;
$Imag_Save_Path=’E:/Picture/’.$ImgName;
echo $Full_Image_Path ;
echo ‘<br />’;

$sourcecode=GetImageFromUrl($Full_Image_Path);

$savefile = fopen($Imag_Save_Path, ‘w’);
fwrite($savefile, $sourcecode);
fclose($savefile);

}

}
?>
<a href=”index.html”>Back</a>