PHP implementation Sina Short link calling API code

Source: Internet
Author: User
Tags comments curl json trim
The code is as follows Copy Code

<?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);
?>

The short connection in this article has already been shipped with a appkey you don't have to apply for a key yourself if you need to use your own, you can also replace it yourself. As for usage it depends on you, insert into other programs to use AH? Making a single page that generates a short connection doesn't matter to me. The use of the method is not much said. Recently lazy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.