Saturday, April 11, 2015

[PHP] Simple Email dengan PHP Mailer

11 April 2015

Intro::

Simple send email menggunakan PHPMAILER dengan akun Gmail.

Steps::

Download Script PHPMAILER
1. https://github.com/PHPMailer/PHPMailer
2. install & extract localhost/twitplay/lib/phpmailer/

Simple Parsing html
1. copas script dibawah ini dan jangan lupa mengganti username_gmail, password_gmail, name, to_email

<?php
require 'lib/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;

$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;

//from username
$mail->Username = 'username_gmail';
$mail->Password = 'password_gmail';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'username_gmail@gmail.com';
$mail->FromName = 'name';

//to email
$mail->addAddress('to_email@example.com');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

//attachment
//$mail->addAttachment('/var/tmp/file.tar.gz');
$mail->addAttachment('img/yupi.jpg');
$mail->isHTML(true);

//body
$mail->Subject = 'Hello World';
$mail->Body    = 'Yupi was Here.';
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

?>

2. simpan email.php dan panggil di browser

Catatan::

attachment yupi.jpg jangan lupa dimasukan kedalam folder /img/

[PHP] Simple Remote Imageshack Upload

11 April 2015

Intro::

Simple Remote Imageshack Upload menggunakan remote image uploader by ptcong

Steps::

Download Script Remote Image Uploader
1. https://github.com/ptcong/php-remote-image-uploader
2. install & extract localhost/twitplay/lib/remoteupload/

Buat Applikasi Imageshack mendapatkan api key
1. https://imageshack.com/contact/api
2. isi form kemudian catat api_key

Simple Upload Imageshack
1. copas script dibawah ini dan jangan lupa mengganti username, password, api_key

<?php
include 'lib/remoteupload/autoload.php';
include 'lib/remoteupload/ChipVN/ClassLoader/Loader.php';
ChipVN_ClassLoader_Loader::registerAutoLoad();

$uploader = ChipVN_ImageUploader_Manager::make('Imageshack');
$uploader->login('username', 'password');
$uploader->setApi('api_key');

//dir upload
echo $uploader->upload(getcwd(). '/img/yupi.jpg');

//link upload
//echo $uploader->transload('http://img33.imageshack.us/img33/6840/wz7u.jpg');


?>

2. simpan testupload.php dan panggil di browser

Catatan::

Jangan lupa menginstall class loader dalam /twitplay/lib/remoteupload/
>https://github.com/ptcong/php-chipvn-classloader
>https://github.com/ptcong/php-http-client
>https://github.com/ptcong/php-cache-manager

[PHP] Simple HTML Parser

11 April 2015
Inspect Element

Intro::


Simple HTML parser, pake Simple DOM HTML Parser.

Steps::

Download Script DOM Html Parsser
1. http://simplehtmldom.sourceforge.net/
2. install & extract localhost/twitplay/lib/dom/

Simple Parsing html
1. copas script dibawah ini untuk parsing Berita Terbaru dari official jkt48

<meta charset="UTF-8">
<?php include "lib/dom/simple_html_dom.php"; 

// Create DOM from URL or file
$url = 'http://jkt48.com/news/list?lang=id';
$html = file_get_html($url);

//set counter
$c = 1;

// Find all post
foreach($html->find('div[class=post]') as $element){
foreach($element->find('div[class=contentpink]') as $element2){
// $c as counter
echo $c.'. ';
echo $element2->find('div[class=excerpt]')[0]->plaintext;
echo ' ';
echo $element2->find('div[class=metadata]')[0]->innertext;
echo '<br><br>';
$c++;
}
}

?>

2. simpan beritajkt.php dan panggil di browser

Catatan::

Simple html dom parser manual : http://simplehtmldom.sourceforge.net/manual.htm

Friday, April 10, 2015

[PHP] Twitter POST API simple

10 April 2015

Intro::

Simple POST update status example buat twitter API via PHP, menggunakan script Abraham.

Steps::

Buat Applikasi Twitter untuk mendapatkan token & secret code
1. https://apps.twitter.com/
2. copy & catat, consumer keyconsumer secretaccess tokenaccess token secret

Download Script TwitterOAuth PHP (versi Abraham)
1. https://github.com/abraham/twitteroauth
2. install & extract localhost/twitplay/lib/twitteroauth/

Simple Post Update
1. copas script dibawah ini dan jangan lupa mengganti consumer keyconsumer secretaccess tokenaccess token secret

<?php include "lib/twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

$consumer = "consumer key";
$consumersecret = "consumer secret";
$accesstoken = "access token";
$accesstokensecret = "access token secret";
$twitter = new TwitterOAuth($consumer, $consumersecret, $accesstoken, $accesstokensecret);
$content = $twitter->get("account/verify_credentials");

//POST UPDATE
$update = 'Testing post via API cc: @radenfaris';
if (strlen($update) < 140){
$updates = $twitter->post("statuses/update", array("status" => $update));
echo "Success updateted - ".$update;
}

?>

2. simpan posttweet.php dan panggil dengan /localhost/twitplay/posttweet.php



Simple Post Update dengan image
1. copas script dibawah ini dan jangan lupa mengganti consumer keyconsumer secretaccess tokenaccess token secret

<?php include "lib/twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

$consumer = "consumer key";
$consumersecret = "consumer secret";
$accesstoken = "access token";
$accesstokensecret = "access token secret";
$twitter = new TwitterOAuth($consumer, $consumersecret, $accesstoken, $accesstokensecret);
$content = $twitter->get("account/verify_credentials");

//tweets
$update = 'Test update via API dengan image cc: @radenfaris';

//image path
$image = 'img/indah.jpg';
if (strlen($update) < 120){
$media = $twitter->upload('media/upload', array('media' => $image));
$param = array(
'status' => $update,
'media_ids' => implode(',', array($media->media_id_string)),
);
$updates = $twitter->post('statuses/update', $param);
echo "Success updateted - ".$update;
}


?>

2. simpan postimage.php dan panggil dengan /localhost/twitplay/postimage.php

Catatan::

yupi.jpg jangan lupa dimasukan kedalam folder /img/Twitter Rest API : https://dev.twitter.com/rest/public
TwitterOAuth PHP Abraham : https://github.com/abraham/twitteroauth

[PHP] Twitter GET API simple

10 April 2015

Intro::

Simple GET timeline example buat twitter API via PHP, menggunakan script Abraham.

Steps::

Buat Applikasi Twitter untuk mendapatkan token & secret code
1. https://apps.twitter.com/
2. copy & catat, consumer key, consumer secret, access token, access token secret

Download Script TwitterOAuth PHP (versi Abraham)
1. https://github.com/abraham/twitteroauth
2. install & extract localhost/twitplay/lib/twitteroauth/
3. copas script dibawah ini dan jangan lupa mengganti consumer keyconsumer secretaccess tokenaccess token secret

<?php include "lib/twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

$consumer = "consumer key";
$consumersecret = "consumer secret";
$accesstoken = "access token";
$accesstokensecret = "access token secret";
$twitter = new TwitterOAuth($consumer, $consumersecret, $accesstoken, $accesstokensecret);
$content = $twitter->get("account/verify_credentials");

//query twitter
$tweets = $twitter->get("statuses/user_timeline",
array( 'screen_name' => 'radenfaris',
'count'=>20,
));

$c=1;

foreach($tweets as $tweet){
echo $c.'. '.$tweet->text.'<br>';
$c++;
}


?>

4. simpan mytweet.php dan panggil dengan /localhost/twitplay/mytweet.php

Catatan::

Twitter Rest API : https://dev.twitter.com/rest/public

Thursday, April 9, 2015

[PHP] Wamp Server Test

9 April 2015

Intro::

Instalasi Apache web server di windows menggunakan wamp server. Wamp server sudah termasuk PHP5, MYSQL, PHPMYADMIN

Steps::

Download dari web resmi Wamp
1. http://www.wampserver.com/en/
2. pilih download versi 32bit/64bit
3. Install
4. Buka applikasi wamp server dan put it online

Test php
1. Tulis script dibawah ini
<?php phpinfo(); ?> 
2. Simpan dengan nama testphp.php di folder
C:\wamp\www\
3. Open browser dengan URL
http://localhost/testphp.php

Test PhpMyAdmin
1. Terinstall pada /phpmyadmin
http://localhost/phpmyadmin
Test MySQL

1. Buka PHPmyadmin dengan membuka halaman
http://localhost/phpmyadmin
2. Buat database dengan nama myoshi
3. pilih database myoshi kemudian pilih import
4. upload myoshi.sql dan klik go

PHP-MYSQL
1. buat file dengan nama myoshi.php di localhost (C:\wamp\www\)
paste kode dibawah ini

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myoshi";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}
$result = $conn->query('SELECT * FROM profil WHERE oshimen=1');
while($row = $result->fetch_assoc()){
echo 'Oshimen: '.$row["nama"];
}
?>

Catatan::

Diperhatikan servername dan password default adalah root dan 'blank'.