在PHP中使用API

要在PHP程序中使用API,您必须通过file_get_contents或cURL发送GET请求:两者都是可靠的方法,您可以直接复制下面的代码。

<?php
/**** Sample PHP Function ***/
function shorten($url, $custom = "", $format = "json") {
$api_url = "https://www.xyixy.com/api/?key=API key";
$api_url .= "&url=".urlencode(filter_var($url, FILTER_SANITIZE_URL));
if(!empty($custom)){
$api_url .= "&custom=".strip_tags($custom);
}
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $api_url
));
$Response = curl_exec($curl);
curl_close($curl);
if($format == "text"){
$Ar = json_decode($Response,TRUE);
if($Ar["error"]){
return $Ar["msg"];
}else{
return $Ar["short"];
}
}else{
return $Response;
}
}
?>

简单用法

<?php
echo shorten("https://baidu.com");
?>

使用自定义短网址

<?php
echo shorten("https://baidu.com", "baidu");
?>

使用自定义短网址和json格式

<?php
echo shorten("https://baidu.com", "baidu", "json");
?>


												

最新文章

  1. 工作总结_js
  2. [Unity3d]游戏中子弹碰撞的处理
  3. jsp_Scriptlet
  4. IT电子书网站下载
  5. 【转】c#实现文件下载
  6. samsung-smart app 开发
  7. BZOJ 4636 蒟蒻的数列
  8. json解析之jackson ObjectMapper
  9. AngularJS(2)-Scope作用域和控制器
  10. form表单和表格
  11. Xcode7 新添旧版模拟器方法
  12. Jeesite 自定义api接口 404 访问不到页面
  13. 第六篇 - bs4爬取校花网
  14. cs231n笔记(二) 最优化方法
  15. SSM框架之整合(Maven实例)
  16. WPF应用Access数据库
  17. keepalive主从上同时出现VIP,且均无法消失
  18. Window下安装npm
  19. Alpha 冲刺报告(8/10)
  20. 如何安装ioncube扩展对PHP代码加密

热门文章

  1. Nginx学习——proxy_pass
  2. nuxt header 设置
  3. kernel32 的 GetVersionExA/W
  4. 使用PHP如何去除字符串结尾的字符
  5. OAuth2.0实例说明
  6. vs2013 命名空间“Microsoft.Office”中不存在类型或命名空间名称“Interop”。是否缺少程序集引用?
  7. 2019-5-16-WPF-光标初始化的时候-temp-文件夹满了无法创建
  8. Pregel的应用实例——单源最短路径
  9. win10无法设置移动热点的一种解决办法
  10. Spring中使用到的设计模式