XYIXY.COM短网址在线生成,快速、稳定、永久有效,免费开放网址缩短API接口。
2024-09-24 17:18:41
在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");
?>
最新文章
- 工作总结_js
- [Unity3d]游戏中子弹碰撞的处理
- jsp_Scriptlet
- IT电子书网站下载
- 【转】c#实现文件下载
- samsung-smart app 开发
- BZOJ 4636 蒟蒻的数列
- json解析之jackson ObjectMapper
- AngularJS(2)-Scope作用域和控制器
- form表单和表格
- Xcode7 新添旧版模拟器方法
- Jeesite 自定义api接口 404 访问不到页面
- 第六篇 - bs4爬取校花网
- cs231n笔记(二) 最优化方法
- SSM框架之整合(Maven实例)
- WPF应用Access数据库
- keepalive主从上同时出现VIP,且均无法消失
- Window下安装npm
- Alpha 冲刺报告(8/10)
- 如何安装ioncube扩展对PHP代码加密
热门文章
- Nginx学习——proxy_pass
- nuxt header 设置
- kernel32 的 GetVersionExA/W
- 使用PHP如何去除字符串结尾的字符
- OAuth2.0实例说明
- vs2013 命名空间“Microsoft.Office”中不存在类型或命名空间名称“Interop”。是否缺少程序集引用?
- 2019-5-16-WPF-光标初始化的时候-temp-文件夹满了无法创建
- Pregel的应用实例——单源最短路径
- win10无法设置移动热点的一种解决办法
- Spring中使用到的设计模式