这篇文章将为大家详细讲解有关使用typecho插件怎么对接口进行调用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
/**
* 发送数据
* @param $url 准备发送的url
* @param $options 系统配置
*/
public static function send_post($url, $options){
//获取API
$api = $options->plugin('BaiduSubmitTest')->api;
//准备数据
if( is_array($url) ){
$urls = $url;
}else{
$urls = array($url);
}
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urls),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
//记录日志
file_put_contents('/tmp/send_log', date('H:i:s') . $result . "\n");
}
由于我们还没有建立日志系统,所以我们将日志先写入文件,先看效果吧!
返回值:
复制代码 代码如下:
{"remain":48,"success":1}
Good!看来没有什么问题!不过为了保险起见,我还是用typecho自带的http类重写了此方法。
public static function send_post($url, $options){
//获取API
$api = $options->plugin('BaiduSubmitTest')->api;
//准备数据
if( is_array($url) ){
$urls = $url;
}else{
$urls = array($url);
}
//为了保证成功调用,老高先做了判断
if (false == Typecho_Http_Client::get()) {
throw new Typecho_Plugin_Exception(_t('对不起, 您的主机不支持 php-curl 扩展而且没有打开 allow_url_fopen 功能, 无法正常使用此功能'));
}
//发送请求
$http = Typecho_Http_Client::get();
$http->setData(implode("\n", $urls));
$http->setHeader('Content-Type','text/plain');
$result = $http->send($api);
//记录日志
file_put_contents('/tmp/send_log', date('H:i:s') . $result . "\n");
}
}
关于使用typecho插件怎么对接口进行调用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。