作者taiwancat (月季)
看板PHP
標題[分享]0rz.net短網址的php函數
時間Tue Jun 20 23:15:44 2006
因為我自己站上要用...就寫了一個
分享出來給有需要的人用o.o
string shorten_url(string)
基本上 就是傳入長網址 傳回短網址
結束o.o
<?php
function shorten_url($url)
{
$post_data = array();
$post_data['url'] = $url;
$cgi_url = "
http://0rz.net/create.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $cgi_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
ob_start();
$result = curl_exec($ch);
$html = ob_get_contents();
ob_end_clean();
$start = strpos($html, 'input type=hidden name=xxurl value="') +
strlen('input type=hidden name=xxurl value="');
$end = strpos($html, '"></form>');
$length = $end - $start;
$shortened_url = substr($html, $start, $length);
return $shortened_url;
}
echo shorten_url("
http://www.super.long.url.here");
?>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.162.73.15
推 suckerlove:推喔~ 06/20 23:32
推 bigair:沒得測試,不過針對這種分享精神大推~ 06/21 00:00
推 leecoco:推推推 06/21 00:24
推 ileadu:您真是好人一枚,雖然目前我還看不懂怎麼用… 06/21 01:07
推 qrtt1:樓上那裡不明白@@? 06/21 05:33
推 ast9869:讚!~ 06/21 11:43
推 gaekeamql:要執行因該要開php_curl.dll 吧!~不過我打開APACHE 06/21 14:50
→ gaekeamql:不能執行 06/21 14:51
推 taiwancat:有什麼錯誤訊息嗎?我自己兩台機器都可以跑 06/21 16:03
推 Lacm:不會用+1 06/21 21:58
→ taiwancat:這行就是用法啦o.o 06/21 23:32
推 previa:空白+1 06/22 06:38
推 taiwancat:網址不能太短 不然他不會讓你縮 06/22 09:50
推 Janoss:推~ 06/22 10:33
推 joaoko:我也推!分享的精神最高尚 06/22 22:13
推 Lacm:@@第八行出錯 它說的@@? 06/23 05:26
推 taiwancat:要開curl模組才能用唷 06/23 09:42
推 BlackCrazy:不會開@@ 06/30 02:15