Re: [分享]0rz.net短網址的php函數
嘿嘿! 俺也分享一下。如果你沒有curl但是有socket可以用啦XD
code: http://nopa.csie.org/c8ad1
======================================================================
function tinyurl($url){
// create socket and connect to tinyurl.com
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($sock, "tinyurl.com", 80) or die ("connection failure!");
#$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";
// send http talk
socket_write($sock, $talk);
// receive html
$recv_len = 1;
$html = "";
while($recv_len >0 ){
$recv_len = socket_recv($sock, $data, 512, 0);
$html .= $data;
}
socket_close($sock);
// 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];
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 163.26.34.105
→
06/26 17:35, , 1F
06/26 17:35, 1F
→
06/26 17:51, , 2F
06/26 17:51, 2F
→
06/26 18:03, , 3F
06/26 18:03, 3F
推
06/26 18:15, , 4F
06/26 18:15, 4F
推
06/27 13:51, , 5F
06/27 13:51, 5F
討論串 (同標題文章)
完整討論串 (本文為第 3 之 4 篇):