PHP generates short URL link in t. cn format by calling Sina API

  • 2021-11-29 06:18:35
  • OfStack

In this paper, an example is given to describe the method that PHP generates short URL links in t. cn format by calling Sina API. Share it for your reference, as follows:

Sina provides API, which converts long links into short links, and can convert long links into short links in the format of t. cn/xxx.

API:

http://api. t. sina. com. cn/short _ url/shorten. json (return result in JSON format)
http://api. t. sina. com. cn/short _ url/shorten. xml (the return result is in XML format)

Request parameters:

The AppKey assigned when source applies for an application represents the only identity of the application when calling the interface.
url_long Long links required for conversion, URLencoded required, no more than 20.
Multiple url parameters need to be requested in the following way: url_long=aaa & url_long=bbb

Create an source method

1. Enter http://open.weibo.com/and select Menu Microlink- > Website access.
2. Click Access Now to create a new application, fill in the application name casually, and click Create.
3. After successful creation, AppKey is the value of the source parameter, which can be used to request the creation of a short link.

Test code:


<?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 = ' What you applied for AppKey';
$url_long = 'https://www.ofstack.com/';
$request_url = sprintf($api.'?source=%s&url_long=%s', $source, $url_long);
$data = file_get_contents($request_url);
echo $data;
?>

Returns JSON format


[
  {
    "url_short": "http:\/\/t.cn\/Rki0twp",
    "url_long": "http:\/\/www.cnblogs.com\/daxiangxm",
    "type": 0
  }
]

Returns XML format


<?xml version="1.0" encoding="UTF-8"?><urls>
  <url>
    <url_short>http://t.cn/RBclsRo</url_short>
    <url_long>https://www.ofstack.com/</url_long>
    <type>0</type>
  </url></urls>

The generated short link is http://t.cn/RBclsRo, and the access will jump to https://www.ofstack.com/

The complete invocation method is as follows:


<?php/**
 *  Call Sina interface to turn long links into short links 
 * @param string    $source   Apply for application AppKey
 * @param array|string $url_long  Long link, supports multiple transformations (needs to be executed first urlencode)
 * @return array
 */function getSinaShortUrl($source, $url_long){
  //  Parameter check 
  if(empty($source) || !$url_long){<br>    return false;
  }  //  Parameter processing, string to array 
  if(!is_array($url_long)){<br>    $url_long = array($url_long);
  }  //  Splice url_long Parameter request format 
  $url_param = array_map(function($value){
    return '&url_long='.urlencode($value);
  }, $url_long);<br>  $url_param = implode('', $url_param);
  //  Sina generates short link interface 
  $api = 'http://api.t.sina.com.cn/short_url/shorten.json';  //  Request url
  $request_url = sprintf($api.'?source=%s%s', $source, $url_param);  <br>  $result = array();  //  Execute request 
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_URL, $request_url);  <br>    $data = curl_exec($ch);<br>  if($error=curl_errno($ch)){<br>  return false;
  }
  curl_close($ch);  $result = json_decode($data, true);  return $result;
}
//AppKey <br>$source = ' What you applied for AppKey';<br>//  Single link conversion 
$url_long = 'https://www.ofstack.com/';<br>$data = getSinaShortUrl($source, $url_long);
print_r($data);<br>//  Multiple link conversion 
$url_long = array('https://www.ofstack.com/','https://www.ofstack.com/','https://www.ofstack.com/');
$data = getSinaShortUrl($source, $url_long);
print_r($data);
?>

Output:

Array(
[0] = > Array
(
[url_short] = > http://t.cn/RBclsRo
[url_long] = > https://www.ofstack.com/
[type] = > 0
)
)Array(
[0] = > Array
(
[url_short] = > http://t.cn/RBclsRo
[url_long] = > https://www.ofstack.com/
[type] = > 0
)
[1] = > Array
(
[url_short] = > http://t.cn/RBclsRo
[url_long] = > https://www.ofstack.com/
[type] = > 0
)
[2] = > Array
(
[url_short] = > http://t.cn/RBclsRo
[url_long] = > https://www.ofstack.com/
[type] = > 0
)
)

After testing, this generation interface is relatively stable!

PS: Here we recommend a short URL generation tool for this site (also a short URL generated by the third party API interface)

Short chain (short website) online generation tool:
http://tools.ofstack.com/password/dwzcreate

For more readers interested in PHP related contents, please check the topics of this site: "Summary of php curl Usage", "Summary of PHP Network Programming Skills", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Tutorial on PHP Data Structure and Algorithm" and "Summary of json Format Data Operation Skills in PHP"

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


Related articles: