顯示文章

這裡允許您檢視這個會員的所有文章。請注意, 您只能看見您有權限閱讀的文章。


文章 - a912335

頁: [1]
1
PHP程式設計討論區 / php curl 使用
« 於: 2013-11-20 00:48 »
我只想取得google網頁上2002股票數據方框
可是不知為什麼就是無法跑出來
是我哪裡寫錯了?
請各位程式高手幫幫忙~~~

<?php   
header("Content-Type: text/html; charset=big5");
$url = "https://www.google.com.tw/search?newwindow=1&biw=1280&bih=908&q=2002&oq=2002&gs_l=serp.3..0l10.226330.226946.0.227234.4.4.0.0.0.0.83.276.4.4.0....0...1c.1.31.serp..0.4.276.pbIQyeh6GVI";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Bee-ma");
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Google Bot");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//在需要用戶檢測的網頁里需要增加下面兩行
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
//curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD);
$contents = curl_exec($ch);
curl_close($ch);
preg_match('/<li[^>]*class="mod g tpo knavi obcontainer"[^>]*>(.*?)<\/li>/Ui',$contents,$match);


$title = $match[1];

echo "$title<BR>";
?>

2
這是我用php收集的新聞
請問我要如何才能把這些新聞每天自動存進資料庫裡(mysql、sqlite)
存入資料庫的方法該如何寫?
請各位程式碼高手幫幫忙~~
謝謝!!!

<?php
//RSS源地址列表數組
$rssfeed = array("http://tw.stock.yahoo.com/rss/url/d/e/N1.html","http://rss.chinatimes.com/rss/stock-u.rss","http://udn.com/udnrss/stock.xml");
//$nn = $rssfeed->getNumberOfNews();

//設置編碼為UTF-8
header('Content-Type:text/html;charset= UTF-8');

for($i=0;$i<sizeof($rssfeed);$i++){//分解開始
$buff = "";
$rss_str="";
//打開rss地址,並讀取,讀取失敗則中止
$fp = fopen($rssfeed[$i],"r") or die("can not open $rssfeed");
while ( !feof($fp) ) {
$buff .= fgets($fp,4096);
}
//關閉文件打開
fclose($fp);

//建立一個 XML 解析器
$parser = xml_parser_create();
//xml_parser_set_option -- 為指定 XML 解析進行選項設置
xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
//xml_parse_into_struct -- 將 XML 數據解析到數組$values中
xml_parse_into_struct($parser,$buff,$values,$idx);
//xml_parser_free -- 釋放指定的 XML 解析器
xml_parser_free($parser);

foreach ($values as $val) {
$tag = $val["tag"];
$type = $val["type"];
$value = $val["value"];
//標籤統一轉為小寫
$tag = strtolower($tag);

if ($tag == "item" && $type == "open"){
$is_item = 1;
}else if ($tag == "item" && $type == "close") {
//構造輸出字符串
$rss_str .= "<h3><a href='".$link."' target=_blank>".$title."</a></h3><span>(".$pubdate.")</span><p>".$description."</p>";
$is_item = 0;
}
//僅讀取item標籤中的內容
if($is_item==1){
if ($tag == "title") {$title = $value;}
if ($tag == "link") {$link = $value;}
if ($tag == "pubdate") {$pubdate = $value;}
if ($tag == "description") {$description = $value;}


}
}
//輸出結果
echo '<p>'.$rss_str.'</p>';
}
?>

頁: [1]