Re: [分享]0rz.net短網址的php函數

看板PHP作者 (愚人)時間18年前 (2006/06/26 17:22), 編輯推噓2(203)
留言5則, 4人參與, 最新討論串3/4 (看更多)
嘿嘿! 俺也分享一下。如果你沒有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
原來#也可以當註解啊, 寫完了才發現>"< 最近perl寫太多了xd
06/26 17:35, 1F

06/26 17:51, , 2F
不過大部分都是用// 當註解 推測原因#不太好按 :p
06/26 17:51, 2F

06/26 18:03, , 3F
我按習慣了orz..整整寫了二週的perl不習慣也都慣了>"<
06/26 18:03, 3F

06/26 18:15, , 4F
原來socket也可以這樣寫! Orz~
06/26 18:15, 4F

06/27 13:51, , 5F
推一個
06/27 13:51, 5F
文章代碼(AID): #14dwTlfr (PHP)
文章代碼(AID): #14dwTlfr (PHP)