此篇我们开始调用接口,我们在插件类中新定义一个方法,起名为send_post,在方法中我们通过系统配置获取接口调用地址。
百度给的例子中使用了php的CURL,更高级的使用方法可以学习PHP_cURL初始化和执行方法
下面我们结合一下百度站长提供的代码。
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/**
* 发送数据
* @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}
{"remain":48,"success":1}
Good!看来没有什么问题!不过为了保险起见,我还是用typecho自带的http类重写了此方法。
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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");
}
}
|
现在我们的插件基本能够运行了,但是在结构上还可以进一步优化!
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 79
-
2025-05-27 36
-
2025-05-27 101
-
2025-05-25 31
-
2025-05-29 82
热门评论

