新浪提供了長連結轉為短連結的API,可以把長連結轉為 t.cn/xxx 這種格式的短連結。
API:
http://api.t.sina.com.cn/short_url/shorten.json (傳回結果是JSON格式)
http://api.t.sina.com.cn/short_url/shorten.xml (傳回結果是XML格式)
##請求參數:
#source 申請應用程式時指派的AppKey,呼叫介面時代表應用程式的唯一身分。
url_long 需要轉換的長鏈接,需要URLencoded,最多不超過20個。
url_long=aaa&url_long=bbb
建立source方法 1.進入http://open.weibo.com/ ,選擇選單微連接->網站存取。
2.點選立即接入,建立新應用,隨便填寫應用程式名稱,點選建立。
3.創建成功後,AppKey就是source參數的值,可以用來要求建立短連結。
測試程式碼:
<?php$api = 'http://api.t.sina.com.cn/short_url/shorten.json'; // json// $api = 'http://api.t.sina.com.cn/short_url/shorten.xml'; // xml$source = '您申请的AppKey';$url_long = 'http://blog.csdn.net/fdipzone';$request_url = sprintf($api.'?source=%s&url_long=%s', $source, $url_long);$data = file_get_contents($request_url);echo $data;?>傳回JSON格式
[ { "url_short": "http:\/\/t.cn\/RyVmU5i", "url_long": "http:\/\/blog.csdn.net\/fdipzone", "type": 0 } ]傳回XML格式
<?xml version="1.0" encoding="UTF-8"?><urls> <url> <url_short>http://t.cn/RyVmU5i</url_short> <url_long>http://blog.csdn.net/fdipzone</url_long> <type>0</type> </url></urls>產生的短連結為http://t.cn/RyVmU5i ,訪問會跳到http://blog.csdn.net/fdipzone
完整呼叫方法如下:##<?php/**
* 调用新浪接口将长链接转为短链接
* @param string $source 申请应用的AppKey
* @param array|string $url_long 长链接,支持多个转换(需要先执行urlencode)
* @return array
*/function getSinaShortUrl($source, $url_long){
// 参数检查
if(empty($source) || !$url_long){ return false;
} // 参数处理,字符串转为数组
if(!is_array($url_long)){ $url_long = array($url_long);
} // 拼接url_long参数请求格式
$url_param = array_map(function($value){
return '&url_long='.urlencode($value);
}, $url_long); $url_param = implode('', $url_param);
// 新浪生成短链接接口
$api = 'http://api.t.sina.com.cn/short_url/shorten.json'; // 请求url
$request_url = sprintf($api.'?source=%s%s', $source, $url_param); $result = array(); // 执行请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $request_url); $data = curl_exec($ch); if($error=curl_errno($ch)){ return false;
}
curl_close($ch); $result = json_decode($data, true); return $result;
}// AppKey$source = '您申请的AppKey';// 单个链接转换$url_long = 'http://blog.csdn.net/fdipzone';$data = getSinaShortUrl($source, $url_long);
print_r($data);// 多个链接转换$url_long = array( 'http://blog.csdn.net/fdipzone/article/details/46390573', 'http://blog.csdn.net/fdipzone/article/details/12180523', 'http://blog.csdn.net/fdipzone/article/details/9316385');$data = getSinaShortUrl($source, $url_long);
print_r($data);?>
輸出:
Array( [0] => Array ( [url_short] => http://t.cn/RyVmU5i [url_long] => http://blog.csdn.net/fdipzone [type] => 0 ) )Array( [0] => Array ( [url_short] => http://t.cn/R4qB08y [url_long] => http://blog.csdn.net/fdipzone/article/details/46390573 [type] => 0 ) [1] => Array ( [url_short] => http://t.cn/RGgNanY [url_long] => http://blog.csdn.net/fdipzone/article/details/12180523 [type] => 0 ) [2] => Array ( [url_short] => http://t.cn/R7TrNWZ [url_long] => http://blog.csdn.net/fdipzone/article/details/9316385 [type] => 0 ) )
本篇文章解釋了透過php 呼叫新浪API產生短鏈接,更多相關內容請關注php中文網。
相關推薦:
解說mysql group by 群組內排序的方法如何利用php 來反射API取得類別資訊詳解在mysql查詢時,offset過大影響效能的原因與最佳化方法以上是如何透過php 呼叫新浪API產生短鏈接的詳細內容。更多資訊請關注PHP中文網其他相關文章!

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考慮使用AttActAcks.s.s.4)

會話再生是指在用戶進行敏感操作時生成新會話ID並使舊ID失效,以防會話固定攻擊。實現步驟包括:1.檢測敏感操作,2.生成新會話ID,3.銷毀舊會話ID,4.更新用戶端會話信息。

PHP会话对应用性能有显著影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版
中文版,非常好用

SublimeText3 Linux新版
SublimeText3 Linux最新版

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。