PHP call Sina Short link API method, PHP call Sina api_php Tutorial

Source: Internet
Author: User

PHP call Sina Short link API method, PHP call Sina API


The example in this article describes how PHP calls the Sina Short link API. Share to everyone for your reference. Here's how:

Copy CodeThe code is as follows: <?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 URL
curl_setopt ($curl _obj, Curlopt_url, $url);
Attach 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 the curl_exec
curl_setopt ($curl _obj, Curlopt_returntransfer, 1);
Setting the time-out period
curl_setopt ($curl _obj, Curlopt_timeout, 15);
Perform
$result = curl_exec ($curl _obj);
Turn off curl.
Curl_close ($curl _obj);
return $result;
}
Simple handling Url,sina Returns an error for non-canonical addresses that begin with no protocol (/HTTP)
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 URLs
function Sinashortenurl ($long _url) {
Stitching 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 for debugging, you can remove the comments to see what information is returned from the 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]-&GT;URL_SHORT = =]
return false;
Else
return $json [0]->url_short;
}
Get long URLs based on short URLs, this function reuses many of the code in Sinashortenurl to make it easier for you to read the comparison, you can merge two functions yourself
function Sinaexpandurl ($short _url) {
Stitching 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 for debugging, you can remove the comments to see what information is returned from the 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]-&GT;URL_LONG = =]
return false;
Else
return $json [0]->url_long;
}
To shorten the URL
$url = $long; It's up to you to see if you want to shorten the URL or get post data or drop it.
$url = Filterurl ($url);
$short = Sinashortenurl ($url);
$ulong = Sinaexpandurl ($short);
?>
The short link in this article already comes with a appkey no need to apply for the key yourself. Of course you can replace yourself if you need to use your own. As for the usage, it's up to you to insert into other programs and make a single page that generates short connections. The use of the method is not much to say.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/908179.html www.bkjia.com true http://www.bkjia.com/PHPjc/908179.html techarticle PHP call Sina Short link API method, PHP call Sina API This article describes the PHP call Sina Short link API method. Share to everyone for your reference. The method is as follows: Copy Generation ...

  • 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.