<?php Sina App_key Define (' Sina_appkey ', ' 31641035 '); function Curlquery ($url) { Set additional HTTP Headers $addHead = Array ( "Content-type:application/json" ); Initialize curl, of course, you can also use Fsockopen instead $curl _obj = Curl_init (); Set up URLs curl_setopt ($curl _obj, Curlopt_url, $url); Additional head content curl_setopt ($curl _obj, Curlopt_httpheader, $addHead); Whether to output return header information curl_setopt ($curl _obj, Curlopt_header, 0); Returns the result of a curl_exec curl_setopt ($curl _obj, Curlopt_returntransfer, 1); Set timeout time curl_setopt ($curl _obj, Curlopt_timeout, 15); Perform $result = curl_exec ($curl _obj); Turn off curl. Curl_close ($curl _obj); return $result; } Simple processing Url,sina An error for an address that does not begin with a protocol (http://) and is not canonical function Filterurl ($url = ' ") { $url = Trim (Strtolower ($url)); $url = Trim (preg_replace ('/^http:///', ', $url)); if ($url = = ") return false; Else Return UrlEncode (' http://'. $url); } Get short URLs based on long web sites function Sinashortenurl ($long _url) { Mosaic request address, this address you can find in the official documents $url = ' http://api.t.sina.com.cn/short_url/shorten.json?source= '. Sina_appkey. ' &url_long= '. $long _url; GET request Results $result = Curlquery ($url); The following line of comments is used for debugging, you can remove the annotation and see what information is returned from Sina. Print_r ($result); exit (); Parsing JSON $json = Json_decode ($result); Abnormal condition returns false if (Isset ($json->error) | | |!isset ($json [0]->url_short) | | $json [0]->URL_SHORT = =] return false; Else return $json [0]->url_short; } According to the short URL to get long URLs, this function reuses a lot of Sinashortenurl code in order to facilitate your reading comparison, you can merge two functions function Sinaexpandurl ($short _url) { Mosaic request address, this address you can find in the official documents $url = ' http://api.t.sina.com.cn/short_url/expand.json?source= '. Sina_appkey. ' &url_short= '. $short _url; GET request Results $result = Curlquery ($url); The following line of comments is used for debugging, you can remove the annotation and see what information is returned from Sina. Print_r ($result); exit (); Parsing JSON $json = Json_decode ($result); Abnormal condition returns false if (Isset ($json->error) | | |!isset ($json [0]->url_long) | | $json [0]->URL_LONG = =] return false; Else return $json [0]->url_long; } To shorten the URL $url = $long; Here to do it yourself, modify the URL you want to shorten or get the post data or how drop. $url = Filterurl ($url); $short = Sinashortenurl ($url); $ulong = Sinaexpandurl ($short); ?> |