我把qrttl大大的程式碼修改成使用fsockopen這個函式來完成
<?php
function tinyurl($url)
{
$fp = fsockopen("tinyurl.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "connection failed.<br>";
} else {
//$url = 'http://www.google.com.tw';
// set post data
$post_data="url=".urlencode($url);
// make up http talk
$talk = "POST /create.php HTTP/1.0\r\n";
$talk .= "HOST:tinyurl.com\r\n";
$talk .= "Keep-Alive:300\r\n";
$talk .= "Content-Type:application/x-www-form-urlencoded\r\n";
$talk .= "Referer:http://tinyurl.com/\r\n";
$talk .= "Content-Length:".strlen($post_data)."\r\n\r\n";
$talk .= $post_data;
$talk .= "\r\n\r\n";
// receive html
fwrite($fp, $talk);
while (!feof($fp)) {
$html.= fgets($fp, 128);
}
// parsing html get tiny url
$lines = split("\n", $html);
$line_match = preg_grep("/blockquote.*tinyurl\.com/", $lines);
preg_match('/href="(http.*)" target/', array_shift($line_match),$tiny_url);
return $tiny_url[1];
//echo $tiny_url[1];
fclose($fp);
}
}
?>
PS:
因為沒辦法使用socket_create等函式
所以我重安裝mod_php5
但不知為啥沒跳出選單 讓我選 @@
後來去安裝 php5-extensions 在 makefile修改 with socket=yes
安裝後還是一樣不能用
不過發現fsockopen可以用....
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.223.167.109
※ 編輯: chenyen 來自: 61.223.167.109 (07/08 13:43)
推
07/08 14:07, , 1F
07/08 14:07, 1F
討論串 (同標題文章)
完整討論串 (本文為第 4 之 4 篇):