顯示具有 php 標籤的文章。 顯示所有文章
顯示具有 php 標籤的文章。 顯示所有文章

2009年9月6日 星期日

PLURK BOT 範例

這是一個以 自由時報即時新聞 為基礎建立的 php plurk bot 範例
會貼出最新的10條新聞


2009年4月18日 星期六

[PHP 筆記] 發個私噗給自己

沒事發私噗給自己?

雖然 PLURK 有假期模式
但想保卡馬又沒時間噗浪
也不想造成他人的困擾
就想寫個程式
搭配 Windows xp '排定的工作'
成為定時發私噗的噗浪機器人
而這個私噗的內容只有自己可看到。

 

plurk_limit.php

 

<?
header('Content-type:text/html; charset=utf-8');
define('NICKNAME', ''); //輸入 PLURK 的 NICKNAME
define('PASSWORD', ''); //輸入 PLURK 的密碼
$user_id = getid(NICKNAME);
define('USER_ID', "$user_id");
set_time_limit(240);

// login
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, '\plurk_cookie_n.txt');
curl_setopt($ch, CURLOPT_URL, 'http://www.plurk.com/Users/login');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'nick_name='.NICKNAME.'&password='.PASSWORD);
curl_exec($ch);
curl_close($ch);

//

date_default_timezone_set("Asia/Taipei");
$out = date("Y-m-d H:i:s");
echo $out;
post($out);


function post($message){
// post
$ch3 = curl_init();
curl_setopt($ch3, CURLOPT_COOKIEFILE, '\plurk_cookie_n.txt');
curl_setopt($ch3, CURLOPT_URL, 'http://www.plurk.com/TimeLine/addPlurk');
curl_setopt($ch3, CURLOPT_POSTFIELDS, 'qualifier=gives&limited_to=%5B'.USER_ID.'%5D&lang=tr_ch&uid='.USER_ID.'&no_comments=0&content='.$message);
curl_exec($ch3);
curl_close($ch3);
}


function getid($name){
$ourl= 'http://plurk.com/'.NICKNAME;
$fid = file_get_contents($ourl);
preg_match('/user_id\": (.*?)\,/s',$fid,$matches);
$u_id = $matches[1];
return($u_id);
}

?>



 



 



上面

define ('NICKNAME', '1234'); 1234 改為你 PLURK 的 NICKNAME
define ('PASSWORD','5678'); 5678 改為你 PLURK 的密碼
如果噗同樣的內容
可能會被 Plurk 的防洪機制所阻擋
所以我把它設定為報時噗
排程的方式

以 AppServ 為例


首先要讓 php 支援 cURL 模組


 


我的排程是執行
D:\AppServ\php5\php.exe D:\AppServ\www\plurk\test\plurk_limit.php

limit



切記!
每日超過 30 噗主題是會扣卡馬的
所以重複執行時間可不能設定太短喔。

 


順便打個小廣告
歡迎加入我的 機器人噗浪

.



[PHP 筆記]查詢某 PLURK NICKNAME 的 user id

 
首先要定義程式中的 NICKNAME
如我的 NICKNAME
define('NICKNAME', 'never4get');
 
<?
define('NICKNAME', ''); // 在 '' 中輸入要查詢的 NICKNAME
$user_id = getid(NICKNAME);
echo $user_id;

function getid($name){
$ourl= 'http://plurk.com/'.NICKNAME;
$fid = file_get_contents($ourl);
preg_match('/user_id\": (.*?)\,/s',$fid,$matches);
$u_id = $matches[1];
return($u_id);
}

?>

2009年2月17日 星期二

[學習筆記] PHP tinyurl 短縮網址 的 function

忘了之前在哪裡找到的
先記下來當做日後查詢的筆記。

tinyurl 可把冗長的網址簡化為較短的網址
下面為好用的 php function



function tinyurl($url){
return(trim(file_get_contents('http://tinyurl.com/api-create.php?url='.$url)));
}



其中:
$url 為欲轉換的網址
代入 function 後
返回值為轉換後的簡化網址

2008年12月1日 星期一

台灣蘋果壹周刊程式修改

最近一些免費空間(如 http://www.000webhost.com/ )為了安全的理由
禁止了 ini_set() 的指令
而台灣蘋果網站一些頁面的抓取需要設定 user agent
所以今日修改程式
由原來的

ini_set('user_agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');


改為 cURL 的指令

curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)');


.

2008年11月23日 星期日

PortableWebAp 啟用 cURL 模組的方法

1.
修改 WebAp\Program\php\php.ini
用文字編輯程式(UltraEdit or EmEdit)編輯
找到其中的 extension=php_curl.dll 這一行
去除前面的分號 ";"
然後儲存。

2.
下載 http://portablewebap.com/downloads/php5.1.6ext.zip
解壓縮後
找到 php_curl.dll copy 到 WebAp\Program\php\ext\ 下

3.
執行 portablewebap.exe 啟用 server.

在 AppServ 中啟用 cURL 模組的方法

在 WINDOWS 的目錄下
找到 php.ini 這個檔
用文字編輯程式(UltraEdit or EmEdit)編輯
找到其中的 extension=php_curl.dll 這一行
去除前面的分號 ";"
然後儲存,
若仍然無法啟動 cURL 可以按照下面步驟修正。

一、在 ..\AppServ\php5\ 下找到 libeay32.dll 及 ssleay32.dll。
二、在 ..\AppServ\php5\ext\ 下找到 php_curl.dll。
三、將上述三個檔案複製到 %windir%/system32 下。
四、重新啟動伺服器即可。

2008年9月9日 星期二

關於香港壹傳媒報刊

今日香港壹傳媒報刊的來源 xml 檔有所變動
先是以為壹傳媒已經修補了漏洞
原來是換湯不換藥
ha ha
可以繼續看雜誌囉。

2008年8月18日 星期一

Fiddler

今天介紹一個剖析網頁的工具~Fiddler

這個工具我覺得非常的好用而且它是免費的

程式可在 這個網頁 找到 或是直接到 這裡 取得。

 

Fiddler 的用途非常的廣泛

我以查詢 台灣自由時報週末版 的圖片原始位置來做範例:

由上圖就可查出原始圖及放大圖存放的位置。

根據上面查出的位置

我寫了個小程式方便瀏覽及用 iSiloX 製作電子書

 

  1. <?
  2. header('Content-type:text/html; charset=big5');
  3. $issue = $_GET['issue'];
  4. if($issue){
  5. $year = substr($issue,0,4);
  6. $m = (substr($issue,4,2)+1);
  7. $d = substr($issue,6,2);
  8. $day = date("j", mktime(0, 0, 0, 0,$d,0));
  9. $month = date("M",mktime(0, 0, 0,$m,0,0));
  10. $month = strtolower($month);
  11. //echo $day.$month;
  12. $content = 'http://www.libertytimes.com.tw/'.$year.'/new/'.$month.'/'.$day.'/';
  13. }else{
  14. $url = 'http://www.libertytimes.com.tw/menu2.js';
  15. $content = file_get_contents($url);
  16. $content = preg_replace('/(.*?)path = \"(.*?)\"(.*)/s',"http://www.libertytimes.com.tw$2",$content);
  17. }
  18. $weekly = @file_get_contents($content.'weekly.htm');
  19. preg_match('/<td style=\"font-size:14px\">(.*?)<\/td>/s',$weekly,$matches);
  20.  
  21. $out = "<title>生活週報</title><img src=http://www.libertytimes.com.tw/2008/images/img_auto/005/Weekend.gif><br><b>$matches[1]</b><hr>\r\n";
  22. if (!$weekly){
  23.     $out .= "<b>今日週末版未出刊!</b>\r\n";
  24. }else{
  25. for ($i=1;$i<=12;$i++){
  26.     if (($i == "4")||($i == "8")){$i = $i+1;}
  27.     $link = $content."images/w".$i.".jpg";
  28.     $blink = $content."images/w".$i."b.jpg";
  29.     $out .= "<a href=$blink><img src=$link border=0 ></a><br>\r\n";
  30. }
  31. }
  32. $out .="<hr><img src=http://static.flickr.com/60/219160214_e6ed15d559_o.gif>";
  33. echo $out;
  34. ?>


 

 

 

演示: http://never4get.net78.net/ltw/ltweekly.php?issue=20080817

 

至於其他如 MagV 網站的內容也可比照上法查詢。

2008年8月12日 星期二

Time php v 1.1

Time 雜誌網頁 格式有少許更動
昨晚我把原來的程式也更新了
內定 time.php 為 U.S. edition

time.php?edition=asia  為亞洲版
分兩個檔案:

time.php

  1. <?
  2.     header('Content-type:text/html; charset=utf-8');
  3.     $edtion = $_GET['edition'];
  4.     $link = "http://www.time.com/time/magazine";
  5.     $dirlink = dirname($link);
  6.     $mylogo = "<img src=http://static.flickr.com/60/219160214_e6ed15d559_o.gif> <br>\r\n<font size=\"2\">time.php v1.1</font>";
  7.     $content = file_get_contents($link);
  8.     preg_match('/<!-- toc_masterhed -->(.*?)<\/ul>/s',$content,$edt);
  9.     $edition = $edt[1];
  10.     preg_match_all('/img src=\"(.*?)jpg/s',$edition,$cv);
  11.     preg_match_all('/<a href=\"(.*?)\"/s',$edition,$el);
  12.     $uscover = $cv[1][0].'jpg';
  13.     $asiacover = $cv[1][2].'jpg';
  14.     $uslink = $link;
  15.     $asialink = $el[1][1];
  16.     $link = $uslink;
  17.     $cover = $uscover;
  18.     if ($edtion == 'asia'){
  19.         $link = $dirlink.'/..'.$asialink;
  20.         $cover = $asiacover;
  21.     }
  22.     $content = file_get_contents($link);
  23.     preg_match("/<title>(.*)<\/title>/is",$content,$head);
  24.     $line = $head[0];
  25.     $line .= "<b>$head[1]</b><br><img src=$cover>\r\n<hr>\r\n";
  26.     preg_match_all("/<div class=\"toc_seched\">(.*?)<\/div></is",$content,$matches);
  27.     $z = count($matches[0]);
  28.     for ($i=0;$i<$z;$i++){
  29.         $area = $matches[0][$i];
  30.         preg_match("/<div class=\"toc_seched\">(.*?)<\/div>/is",$area,$h);
  31.         $line .= "<br>\r\n<b><font color=red>$h[1]</font></b><br>\r\n<ul>\r\n";
  32.         preg_match_all("/<a class=(.*?)<br \/>/is",$area,$t);
  33.             for ($j=0;$j<count($t[0]);$j++){
  34.                 $title = $t[0][$j];
  35.                 $title = eregi_replace("<br />","",$title);
  36.                 $title = eregi_replace('/time/','time_view.php?link=http://www.time.com/time/',$title);
  37.                 $line .= "<li>$title</li>\r\n";
  38.             }
  39.             $line .= "</ul><br>\r\n";
  40.     }
  41.     $line .= "<hr>\r\n$mylogo";
  42.     echo $line;
  43. ?>




time_view.php

  1. <?
  2.     header('Content-type:text/html; charset=utf-8');
  3.     $link = $_GET['link'];
  4.     $mylogo = "<img src=http://static.flickr.com/60/219160214_e6ed15d559_o.gif> <br>\n<font size=\"2\">time.php v1.1</font>";
  5.     $content = file_get_contents($link);
  6.     preg_match("/<head>(.*?)<\/head>/is",$content,$css);
  7.     preg_match("/<!-- Begin Tout1 -->(.*)<!-- End Tout1 -->/is",$content,$pix);
  8.     preg_match("/<div class=\'print\'><a href=\'(.*?)\'/is",$content,$nl);
  9.     preg_match("/<div class=\"print\"><a href=\"(.*?)\"/is",$content,$nl2);
  10.     if ($nl[1]||$nl2[1]){
  11.         $newlink = $nl[1];
  12.         $newlink .= $nl2[1];
  13.         $newlink = 'http://www.time.com/'.$newlink;
  14.         $newlink = eregi_replace('"',"",$newlink);
  15.         $content = file_get_contents($newlink);
  16.     }
  17.     preg_match("/<title>(.*?)<\/title>/is",$content,$t);
  18.     $title = $t[0];
  19.     $title = eregi_replace("printout","",$title);
  20.     preg_match("/<!-- Begin Tout1 -->(.*?)<!-- Begin Buttons -->/is",$content,$a);
  21.     $area = $a[1];
  22.     $line = $title."\r\n";
  23.     $line .= $area;
  24.     $allowed_tags = "<b>,<p>,<div>,<h1>,<span>,<title>,<br>,<hr>,<img>,<font>";
  25.     $line = strip_tags($line, $allowed_tags);
  26.     $line .=$pix[0];
  27.     $line = preg_replace('/<div id=\"copy\">(.*?)<\/span>/s','',$line);
  28.     $line = preg_replace('/<div class=\"enlarge\">(.*?)<\/div>/s','',$line);
  29.     $line = preg_replace('/<a id=\"enlargeImg\"(.*?)>/s','',$line);
  30.     $line = eregi_replace('a_','',$line);
  31.     $line = preg_replace('/width=(.*?)\//s','/',$line);
  32.     $line .= "<hr>\r\n$mylogo";
  33.     echo $line;
  34. ?>

Webmaster Tools - Advanced

 

U.S. edition 演示

Asia edition 演示

2008年7月7日 星期一

SCMP php 原始檔公開

共分兩個檔
第一個檔為 scmp.php
用於顯示新聞標題

  1. <?
  2. header('Content-type:text/html; charset=utf-8');
  3. $out = "<title>SCMP.com</title>\r\n";
  4. $out .= "<b><font size=5>SCMP</font></b><br>\r\n";
  5. $main = file_get_contents('http://www.scmp.com/portal/site/SCMP/');
  6. preg_match('/<td colspan=\"3\" style="padding-top: 3px; font-size:12px;font-family:Arial\;\" align=\"center\" valign=\"middle\">(.*?)<\/td>/s',$main,$issue);
  7. $out .="$issue[1]<hr>\r\n";
  8. $tt = array('Hong Kong','China','Asia','World','Education');
  9. $l = array('hong_kong','china','asia','world','education');
  10. $baseurl = 'http://www.scmp.com/rss/';
  11. for($i=0;$i<5;$i++){
  12. $url = "$baseurl"."$l[$i].xml";
  13. $content = file_get_contents($url);
  14. if (ereg('<item>',$content)){
  15. $out .= "<p><b><font color=#656565>$tt[$i]</font></b><br>\r\n"
  16. $content = preg_replace('/(.*?)<item>(.*?)<\/item>/s',"$2",$content);
  17. $content = preg_replace('/<pubDate>(.*?)<\/pubDate>/s','',$content);
  18. $content = preg_replace('/<description>(.*?)<\/description>/s','',$content);
  19. $content = preg_replace('/<title>(.*?)<\/title>(.*?)<link>(.*?)<\/link>/s',"<li><a href=scmp_view.php?link=$3>$1</a></li>\r\n",$content);
  20. $content = eregi_replace('<!\[CDATA\[','',$content);
  21. $content = eregi_replace('\]\]>','',$content);
  22. $out .= $content;
  23. }
  24. }
  25. $out .= "<hr><img src=http://static.flickr.com/60/219160214_e6ed15d559_o.gif>";
  26. echo $out;
  27. ?>


第二個檔為 scmp_view.php (2009-3-15 更新)
用於顯示新聞內文

  1. <?
  2.     header('Content-type:text/html; charset=utf-8');
  3.     ini_set('user_agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
  4.     $link = $_GET['link'];
  5.     $content = file_get_contents($link);
  6.     preg_match('/printform_target\">(.*?)<\/form>/s',$content,$match);
  7.     $content = $match[1];
  8.     preg_match('/artHeadline value=\"(.*?)\">/s',$content,$hl);
  9.     preg_match('/artSubHeadline value=\"(.*?)\">/s',$content,$shl);
  10.     preg_match('/artDate value=\"(.*?)\">/s',$content,$ad);
  11.     preg_match('/artByline value=\"(.*?)\">/s',$content,$bl);
  12.     preg_match('/artBody value=\"(.*?)\">/s',$content,$ab);
  13.     $headline = $hl[1];
  14.     $subheadline = $shl[1];
  15.     $art_date = $ad[1];
  16.     $by_line = $bl[1];
  17.     $body = $ab[1];
  18.     $out .= "<title>SCMP.com $headline</title>";
  19.     $out .= "<b><font color=#656565 size=5>$headline</font></b><br>\r\n";
  20.     $out .= "$subheadline<br>\r\n";
  21.     $out .= "$art_date<br>\r\n";
  22.     $out .= "$by_line<hr>\r\n";
  23.     $out .= "$body<hr>\r\n";
  24.     $out = eregi_replace('&lt;','<',$out);
  25.     $out = eregi_replace('&gt;','>',$out);
  26.     $out = eregi_replace('&amp;quot;','"',$out);
  27.     $out .= "<img src=http://static.flickr.com/60/219160214_e6ed15d559_o.gif>";
  28.     $allowed_tags = "<u>,<b>,<li>,<br>,<hr>,<img>,<font>,<p>,<title>";
  29.     $out = strip_tags($out, $allowed_tags);
  30.     echo $out;
  31. ?>


Enjoy!

2008年7月2日 星期三

about SCMP 南華早報


自從 scmp.com 改版後
php 失效已經很久了
昨天瀏覽該網站
發現在每則新聞的原始 html 中
藏有除了圖片之外的完整新聞內容
不需訂戶登入即可截取其中的資料


今天花了點時間把程式寫出

新聞的標題由 rss 中抽取出
演示在下面網址

http://never4get.net78.net/scmp/scmp.php