作者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
1F:推 suckerlove:推喔~ 06/20 23:32
2F:推 bigair:没得测试,不过针对这种分享精神大推~ 06/21 00:00
3F:推 leecoco:推推推 06/21 00:24
4F:推 ileadu:您真是好人一枚,虽然目前我还看不懂怎麽用… 06/21 01:07
5F:推 qrtt1:楼上那里不明白@@? 06/21 05:33
6F:推 ast9869:赞!~ 06/21 11:43
7F:推 gaekeamql:要执行因该要开php_curl.dll 吧!~不过我打开APACHE 06/21 14:50
8F:→ gaekeamql:不能执行 06/21 14:51
9F:推 taiwancat:有什麽错误讯息吗?我自己两台机器都可以跑 06/21 16:03
10F:推 Lacm:不会用+1 06/21 21:58
12F:→ taiwancat:这行就是用法啦o.o 06/21 23:32
13F:推 previa:空白+1 06/22 06:38
14F:推 taiwancat:网址不能太短 不然他不会让你缩 06/22 09:50
15F:推 Janoss:推~ 06/22 10:33
16F:推 joaoko:我也推!分享的精神最高尚 06/22 22:13
17F:推 Lacm:@@第八行出错 它说的@@? 06/23 05:26
18F:推 taiwancat:要开curl模组才能用唷 06/23 09:42
19F:推 BlackCrazy:不会开@@ 06/30 02:15