夜深了,我分享个代码片段-技术论坛-技术-6KE论坛-综合开放交流论坛

夜深了,我分享个代码片段

这个代码片段是采集xml最新文章/帖子推送给你api接口,实现第三方通知。例如本站的微信群机器人通知即是如此

20240730233146402-1722353506-image

 

<?php
// XML 文件 URL
$xml_url = '这里是你XML链接';
// 获取 XML 文件内容
$xml = file_get_contents($xml_url);
// 解析 XML 文件
$xml_obj = simplexml_load_string($xml);
// 遍历 XML 文件中的所有 URL
foreach ($xml_obj->url as $url) {
// 获取文章的链接、发布时间等信息
$link = (string) $url->loc;
$pubDate = strtotime((string) $url->lastmod);
// 这里可以根据需要获取其他信息,比如文章标题、作者等
// 判断文章是否更新
$last_pubDate = file_get_contents('last_pubDate.txt');
if ($pubDate > $last_pubDate) {
// 发送文章链接到指定 API
send_notification($link);
// 保存最新发布时间
file_put_contents('last_pubDate.txt', $pubDate);
// 记录推送信息到本地 txt 文件
$log_file = 'push_log.txt';
$log_data = date('Y-m-d H:i:s') . ' - ' . $link . PHP_EOL;
file_put_contents($log_file, $log_data, FILE_APPEND);
}
}
// 发送通知函数
function send_notification($link) {
// 替换成您自己的 API URL
$api_url = '这里是你推送到第三方的API接口链接';
// 发送文章链接到指定 API
$data = array(
'link' => $link
);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => json_encode($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($api_url, false, $context);
}
?>
<?php
// XML 文件 URL
$xml_url = '这里是你XML链接';

// 获取 XML 文件内容
$xml = file_get_contents($xml_url);

// 解析 XML 文件
$xml_obj = simplexml_load_string($xml);

// 遍历 XML 文件中的所有 URL
foreach ($xml_obj->url as $url) {
  // 获取文章的链接、发布时间等信息
  $link = (string) $url->loc;
  $pubDate = strtotime((string) $url->lastmod);
  // 这里可以根据需要获取其他信息,比如文章标题、作者等
  
  // 判断文章是否更新
  $last_pubDate = file_get_contents('last_pubDate.txt');
  if ($pubDate > $last_pubDate) {
    // 发送文章链接到指定 API
    send_notification($link);
    
    // 保存最新发布时间
    file_put_contents('last_pubDate.txt', $pubDate);
    
    // 记录推送信息到本地 txt 文件
    $log_file = 'push_log.txt';
    $log_data = date('Y-m-d H:i:s') . ' - ' . $link . PHP_EOL;
    file_put_contents($log_file, $log_data, FILE_APPEND);
  }
}

// 发送通知函数
function send_notification($link) {
  // 替换成您自己的 API URL
  $api_url = '这里是你推送到第三方的API接口链接';
  
  // 发送文章链接到指定 API
  $data = array(
    'link' => $link
  );
  $options = array(
    'http' => array(
      'method' => 'POST',
      'header' => 'Content-type: application/json',
      'content' => json_encode($data)
    )
  );
  $context = stream_context_create($options);
  $result = file_get_contents($api_url, false, $context);
}
?>
<?php // XML 文件 URL $xml_url = '这里是你XML链接'; // 获取 XML 文件内容 $xml = file_get_contents($xml_url); // 解析 XML 文件 $xml_obj = simplexml_load_string($xml); // 遍历 XML 文件中的所有 URL foreach ($xml_obj->url as $url) { // 获取文章的链接、发布时间等信息 $link = (string) $url->loc; $pubDate = strtotime((string) $url->lastmod); // 这里可以根据需要获取其他信息,比如文章标题、作者等 // 判断文章是否更新 $last_pubDate = file_get_contents('last_pubDate.txt'); if ($pubDate > $last_pubDate) { // 发送文章链接到指定 API send_notification($link); // 保存最新发布时间 file_put_contents('last_pubDate.txt', $pubDate); // 记录推送信息到本地 txt 文件 $log_file = 'push_log.txt'; $log_data = date('Y-m-d H:i:s') . ' - ' . $link . PHP_EOL; file_put_contents($log_file, $log_data, FILE_APPEND); } } // 发送通知函数 function send_notification($link) { // 替换成您自己的 API URL $api_url = '这里是你推送到第三方的API接口链接'; // 发送文章链接到指定 API $data = array( 'link' => $link ); $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type: application/json', 'content' => json_encode($data) ) ); $context = stream_context_create($options); $result = file_get_contents($api_url, false, $context); } ?>

 

© 免责声明

请登录后发表评论