You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sinaAPI.php 2.9 KiB

3 年之前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Steven
  5. * Date: 2016/8/4
  6. * Time: 14:25
  7. */
  8. //Sina App_Key
  9. define('SINA_APPKEY', '31641035');
  10. //获取请求结果
  11. function curlQuery($url) {
  12. //设置附加HTTP头
  13. $addHead = array(
  14. "Content-type: application/json"
  15. );
  16. //初始化curl,当然,你也可以用fsockopen代替
  17. $curl_obj = curl_init();
  18. //设置网址
  19. curl_setopt($curl_obj, CURLOPT_URL, $url);
  20. //附加Head内容
  21. curl_setopt($curl_obj, CURLOPT_HTTPHEADER, $addHead);
  22. //是否输出返回头信息
  23. curl_setopt($curl_obj, CURLOPT_HEADER, 0);
  24. //将curl_exec的结果返回
  25. curl_setopt($curl_obj, CURLOPT_RETURNTRANSFER, 1);
  26. //设置超时时间
  27. curl_setopt($curl_obj, CURLOPT_TIMEOUT, 15);
  28. //执行
  29. $result = curl_exec($curl_obj);
  30. //关闭curl回话
  31. curl_close($curl_obj);
  32. return $result;
  33. }
  34. //简单处理下url,sina对于没有协议(http://)开头的和不规范的地址会返回错误
  35. function filterUrl($url = '') {
  36. $url = trim(strtolower($url));
  37. $url = trim(preg_replace('/^http:\//', '', $url));
  38. if ($url == '')
  39. return false;
  40. else
  41. return urlencode('http://' . $url);
  42. }
  43. //根据长网址获取短网址
  44. function sinaShortenUrl($long_url) {
  45. $access_url = "https://api-ssl.bitly.com/v3/shorten?access_token=".BITACCESSTOKEN."&longUrl=".$long_url;
  46. $result = httpRequest($access_url);
  47. $return_result = json_decode($result,true);
  48. if( $return_result["status_code"] == 200 ) {
  49. $url = $return_result["data"]["url"];
  50. $return_url = str_replace("http://", "https://", $url);
  51. return $return_url;
  52. } else {
  53. return false;
  54. }
  55. //拼接请求地址,此地址你可以在官方的文档中查看到
  56. $url = 'http://api.t.sina.com.cn/short_url/shorten.json?source=' . SINA_APPKEY . '&url_long=' . $long_url;
  57. //获取请求结果
  58. $result = curlQuery($url);
  59. //下面这行注释用于调试,
  60. //print_r($result);exit();
  61. //解析json
  62. $json = json_decode($result);
  63. //异常情况返回false
  64. if (isset($json->error) || !isset($json[0]->url_short) || $json[0]->url_short == '')
  65. return false;
  66. else
  67. return $json[0]->url_short;
  68. }
  69. //根据短网址获取长网址,此函数重用了不少sinaShortenUrl中的代码,以方便你阅读对比,你可以自行合并两个函数
  70. function sinaExpandUrl($short_url) {
  71. //拼接请求地址,此地址你可以在官方的文档中查看到
  72. $url = 'http://api.t.sina.com.cn/short_url/expand.json?source=' . SINA_APPKEY . '&url_short=' . $short_url;
  73. //获取请求结果
  74. $result = curlQuery($url);
  75. //下面这行注释用于调试
  76. //print_r($result);exit();
  77. //解析json
  78. $json = json_decode($result);
  79. //异常情况返回false
  80. if (isset($json->error) || !isset($json[0]->url_long) || $json[0]->url_long == '')
  81. return false;
  82. else
  83. return $json[0]->url_long;
  84. }
  85. ?>