Monday, 30 November 2009
Update Twitter Status with PHP
function tweetMyMessage($tweetUsername = '', $tweetPassword = '', $tweetMessage = '') {
if (function_exists('curl_init')) {
$twitterUsername = trim($tweetUsername);
$twitterPassword = trim($tweetPassword);
if(strlen($tweetMessage) > 140) {
$tweetMessage = substr($tweetMessage, 0, 140);
}
$twitterStatus = htmlentities(trim(strip_tags($tweetMessage)));
if (!empty($twitterUsername) && !empty($twitterPassword) && !empty($twitterStatus)) {
$strTweetUrl = 'http://www.twitter.com/statuses/update.xml';
$objCurlHandle = curl_init();
curl_setopt($objCurlHandle, CURLOPT_URL, "$strTweetUrl");
curl_setopt($objCurlHandle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($objCurlHandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($objCurlHandle, CURLOPT_POST, 1);
curl_setopt($objCurlHandle, CURLOPT_POSTFIELDS, "status=$twitterStatus");
curl_setopt($objCurlHandle, CURLOPT_USERPWD, "$twitterUsername:$twitterPassword");
$result = curl_exec($objCurlHandle);
$arrResult = curl_getinfo($objCurlHandle);
if ($arrResult['http_code'] == 200) {
echo 'Your Tweet has been posted for update.';
}
else {
echo 'Sorry, could not post your Tweet message to Twitter.';
}
curl_close($objCurlHandle);
}
else {
echo('Missing required parameters to submit your Twitter message.');
}
}
else {
echo('Curl Extension is not installed.');
}
}
$username = 'twitterUsername';
$password = '**********';
$message = 'update my twitter status.';
/* invoking the tweetMyMessage function to Post Twitter from PHP. */
tweetMyMessage($username, $password, $message);
Search Engine-Friendly URL Generator & how to use/write .htaccess file for SEF URL
#LoadModule rewrite_module modules/mod_rewrite.so
uncomment(revode the # char at the beginning of line) the above line as
LoadModule rewrite_module modules/mod_rewrite.so
2. Then goto the root directory of your project, for example, samplesite at location htdocs/samplesite/. Then create the .htaccess (don't forgot to add dot(.) on filename) file on that location as htdocs/samplesite/.htaccess
3. Then write the following lines of code on that .htaccess file as;
/* ------------ .htaccess file ---------*/
---------------------------------------------------------------------------------
Options +FollowSymlinks
RewriteEngine on
RewriteBase /samplesite /* this /samplesite is base url of your project. */
/* then write the rule of your choice, sample rules as below */
RewriteRule searcharticle/(.*)/(.*)/(.*)\.html searcharticle.php?cat=$1&atitle=$2&aid=$3
RewriteRule viewarticle/(.*)/(.*) viewarticle.php?title=$1&aid=$2
---------------------------------------------------------------------------------
Then, save the .htaccess file with above data.
Here,
RewriteRule searcharticle/(.*)/(.*)/(.*)\.html searcharticle.php?cat=$1&atitle=$2&aid=$3
means ( we need to set article search URL in below format as )
http://localhost/samplesite/searcharticle/cat/articleTitle/24.html
=>(implies to )
http://localhost/samplesite/searcharticle.php?cat=catTitle&atitle=ArticleTitle&aid=24
(in normal PHP way without using of .htaccess code)
Similarly,
RewriteRule viewarticle/(.*)/(.*) viewarticle.php?title=$1&aid=$2
means ( we need to set article view URL in below format as )
http://localhost/samplesite/viewarticle/articleTitle/24
=>(implies to )
http://localhost/samplesite/viewarticle.php?atitle=ArticleTitle&aid=24
(in normal PHP way without using of .htaccess code)
This is the way to generate Search Engine-Friendly URL.
Also, here to Generator to make RULES for Search Engine-Friendly URL.
Friday, 27 November 2009
How to Use PHPMailer to send mail, PHPMailer() Class, class.phpmailer.php
2. Include the class.phpmailer.php to the file from where you want to send mail.
3. Make a local library function class of your choice.
for example, the following "func.global.php" as
/* ------------------file name: - func.global.php ---------- */
function sendMailWithAttachment($config, $email_data)
{
$email = $email_data;
$mail = new PHPMailer();
$mail->CharSet = "utf-8";
if($config['email_type'] == 'smtp')
{
$mail->IsSMTP();
$mail->SMTPAuth = TRUE;
$mail->Username = $config['email_username'];
$mail->Password = $config['email_password'];
$mail->Host = $config['email_hostname'];
}
else if ( $config['email_type'] == 'sendmail')
{
$mail->IsSendmail();
}
else { $mail->IsMail(); }
$mail->FromName = $email['from_name'];
$mail->From = $email['from_email'];
if ( count($email['bcc']) > 0)
{
$counter = 0;
foreach ($email['bcc'] as $value)
{
if($counter == 0) { $mail->AddAddress($value); }
else { $mail->AddBCC($value); }
$counter++;
}
} else
{
$mail->AddAddress ( $email['to_email'];
}
if ( !empty($email['attachment']))
{
$mail->AddAttachment ($email['attachemnt']);
}
$mail->IsHTML (FALSE);
$mail->Send();
$sentMessage = array();
$sentMessage['isSent'] = TRUE:
$sentMessage['sentMessage'] = "Message has been sent";
if ( ! $mail->Send())
{
$sentMessage['isSent'] = FALSE;
$sentMessage['sentMessage'] = "Message could not be sent
";
$sentMessage['sentMessage'] .= "Mailer Error : ", $mail->ErrorInfo;
}
return $sentMessage;
}
/* end of sendMailWithAttachment function. */
4. Use the above "func.global.php" file's function called sendMailWithAttachment() as;
/* ------------------file name: - sendEmail.php ---------- */
require_once ( "class.phpmailer.php");
require_once ( "func.global.php");
/* To send the email as normal php mailto() function ways but throw the your own SMTP mail server. */
$config = array('email_type'=>'smtp', 'email_username'=>'smtpuser', 'email_password'=>'smtppswd', 'email_host'=>'hostserver');
/* To send the email as normal php mailto() function ways. */
$config = array('email_type'=>'sendmail');
$email_data = array('from_name'=>'Jastin', 'from_email'=>'jastin.td@gmail.com',
'to_email'=>'kuliek@hotmail.com',
'bcc'=>array('gostels@live.com','hellopotak@yahoo.com'),
'attachement'=>'C:\docs\my_pics.jpeg'
);
sendMailWithAttachment ($config, $email_data);
If you like to read more about the PHP Mailer, click here
Sunday, 22 November 2009
How to Install Magento on XAMPP in XP
XAMPP is an easy to install LAMP environment used for testing. Magento is an open source ecommerce package. You should already have XAMPP installed and a copy of Magento. Start Apache and MySql if not already running.
Enable curl in XAMPP
1) Locate the following files:
C:\Program Files\xampp\apache\bin\php.ini
C:\Program Files\xampp\php\php.ini
2) Uncomment the following line on the php.ini files by removing the semicolon.
;extension=php_curl.dll
3) Restart your apache server
Create Magento database
1) Open a browser and navigate to http://127.0.0.1/phpmyadmin/
2) Create a new data base named magento
Install Magento
1) Extract the Magento-1.0.19870.4. zip file
2) Copy the extracted files to the httpdoc folder of XAMPP (rename any exsisting index files for your XMAPP install)
3) Navigate your browser to http://127.0.0.1/index.php
4) Follow the Magento install wizard
Sunday, 15 November 2009
KTM: HSEB publishes class XI results 2009
Friday, 13 November 2009
To reload the Parent Window from Clild Window by Javascript
< a href="#" onClick="javascript:parent.location.reload()" > Click to Reload Parent window < /a >
Thursday, 12 November 2009
HTML to PDF, generate PDF with HTML data,
1. Download the dompdf from
http://code.google.com/p/dompdf/
or http://sourceforge.net/projects/dompdf/
2. Upzip(if necessary) and keep the dompdf/ folder at your's project root directory.
then, make a function to generate pdf file for download or write a file to the disk as;
/* function to generate the .pdf file for download. */
function get_generate_pdf ($htmlContent, $filename)
{
require_once ("../dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf -> load_html($htmlContent);
$dompdf -> render();
$dompdf -> stream($filename);
}
/* function to write the .pdf file with HTML Contents. */
function get_write_pdf_file ($htmlContent, $filename)
{
require_once ("../dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf -> load_html($htmlContent);
$dompdf -> render();
$pdfContent = $dompdf -> output();
file_put_contents($filename, $pdfContent);
}
/* function to write the file Contents. */
function get_write_file ($content, $filename, $mode="w")
{
$fp = @fopen ($file, $mode);
if ( ! is_resource($fp)) { return false; }
fwrite ($fp, $content);
fclose ($fp);
}
$htmlContent = "< html >< head >< title >SamplePDF file< /title >< /head >
< body >< strong >You can include any kinda html content in this body format.< /strong >< /body >< /html >";
$filename = "sample_pdf_file.pdf";
/* Calling above function for html content in pdf attachment file format. */
get_generate_pdf ($htmlContent, $filename)
/* Calling above function for html content in pdf attachment file format. */
get_generate_pdf ($htmlContent, $filename);
Friday, 6 November 2009
PDF and PHP, Generate PDFs with PHP
include_once ('class.ezpdf.php');
//ezpdf: from http://www.ros.co.nz/pdf/?
//docs: http://www.ros.co.nz/pdf/readme.pdf
//note: xy origin is at the bottom left
//data
$colw = array( 80 , 40, 220, 80, 40 );//column widths
$rows = array(
array('company','size','desc','cost','instock'),
array("WD", "80GB","WD800AAJS SATA2 7200rpm 8mb" ,"$36.90","Y"),
array("WD","160GB","WD1600AAJS SATA300 8mb 7200rpm" ,"$39.87","Y"),
array("WD", "80GB","800jd SATA2 7200rpm 8mb" ,"$41.90","Y"),
array("WD","250GB","WD2500AAKS SATA300 16mb 7200rpm" ,"$49.88","Y"),
array("WD","320GB","WD3200AAKS SATA300 16mb 7200rpm" ,"$49.90","Y"),
array("WD","160GB","1600YS SATA raid 16mb 7200rpm" ,"$59.90","Y"),
array("WD","500GB","500gb WD5000AAKS SATA2 16mb 7200rpm","$64.90","Y"),
array("WD","250GB","2500ys SATA raid 7200rpm 16mb" ,"$69.90","Y"),
);
//x is 0-600, y is 0-780 (origin is at bottom left corner)
$pdf =& new Cezpdf('LETTER');
$image = imagecreatefrompng("background.png");
$pdf->addImage($image,0,0,611);
$pdf->selectFont("fonts/Helvetica.afm");
$pdf->setColor(0/255,0/255,0/255);
$pdf->addText(80,620,10,"List of Hard Drives");
$pdf->setLineStyle(0.5);
$pdf->line(80,615,540,615);
$pdf->setStrokeColor(0,0,0);
$pdf->setColor(0/255,0/255,0/255);
$pdf->addText(30,16,8,"Created ".date("m/d/Y"));
$total=0;
$curr_x=80;
$curr_y=600;
foreach($rows as $r)
{
$xoffset = $curr_x;
foreach($r as $i=>$data)
{
$pdf->setColor(0/255,0/255,0/255);
$pdf->addText( $xoffset, $curr_y , 10, $data );
$xoffset+=$colw[$i];
}
$curr_y-=20;
}
$pdf->ezStream();
?>
Demo:
Download Demo PDF (dynamically generated with php)
Download all-in-one example.zip 349KB (includes ezPDF)
Notes:
* The above code generates a 1 page pdf, but a multi-page PDF is possible using the $pdf->ezNewPage() function. Any $pdf->addText() or $pdf->addImage() calls after that point are added to the new page.
* The size of a LETTER page in ezPDF is 612x792 (w by h in pixels)
* The (x,y) origin (0,0) is at the bottom left of a PDF page
Further Reading
* ezPDF and
* ezPDF API Docs[pdf]
* pdf-php at sourceforge
Thursday, 5 November 2009
Nepali Date, Get Nepali Date, Today's Nepali Date
Nepali Date Sample
(no need to update daily, it will be auto update)
For that Please Contact at :
Email: info@grabhost.net
visit: www.date.grabhost.net
Thank You !!!