php - echo getUrlContent($url) 仅用于最后一个 url
问题描述
我有一个站点 urls 数组(php 文件链接),需要通过循环回显每个,但它只显示循环的最后一个 url 的内容!我使用了 getUrlContent($url) 函数
<?php
$work_fldr = "work_fldr";
$ini_list_fl = $work_fldr.'/site_list.txt';
If (isset($ini_list_fl)){
$site_arr = explode(PHP_EOL, file_get_contents($ini_list_fl));
$default_site = $site_arr[0];
$cntnt_fl = $default_site.'/ptmw/work_fldr/cntnt_fl.txt';
$ini_fl = $default_site.'/ptmw/work_fldr/POST.ini';
for ($i=1;$i<sizeof($site_arr);$i++){
$site_ex = explode('|', $site_arr[$i] );
$site_url = $site_ex[1].'/ptmw/create_post.php?cat='.$site_ex[3].'&usrnm='.$site_ex[2].'&inifl='.$ini_fl.'&cntntfl='.$cntnt_fl;
echo getUrlContent($site_url);
echo '===============================<br>';
}
}
function getUrlContent($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language:en-US,en;q=0.8',
'Cache-Control:max-age=0',
'Connection:keep-alive',
'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36',
]);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
?>
这是示例文件内容:
POST.ini
[0]
Title = "post title"
Featured_img = "image url"
site_list.txt
http://engnajjar2.000webhostapp.com
mhd2020|http://mhd2020.000webhostapp.com|username|1
engnajjar2|http://engnajjar2.000webhostapp.com|username|1
请问如何回显每个网址的内容?
解决方案
我建议使用以下更改更新您的代码:
site_list.txt
包含:
http://engnajjar2.000webhostapp.com
mhd2020|http://mhd2020.000webhostapp.com|username|1
engnajjar2|http://engnajjar2.000webhostapp.com|username|1
PHP 脚本包含:
<?php
$site_arr = explode(PHP_EOL, trim(file_get_contents('site_list.txt')));
$default_site = $site_arr[0];
$cntnt_fl = $default_site.'/ptmw/work_fldr/cntnt_fl.txt';
$ini_fl = $default_site.'/ptmw/work_fldr/POST.ini';
foreach ($site_arr as $key => $site) {
if ($key == 0) {
continue;
}
$site_ex = explode('|', $site );
$site_url = $site_ex[1].'/ptmw/create_post.php?cat='.$site_ex[3].'&usrnm='.$site_ex[2].'&inifl='.$ini_fl.'&cntntfl='.$cntnt_fl;
echo "URL -> ".$site_url."\n";
echo getUrlContent($site_url)."\n";
}
function getUrlContent($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language:en-US,en;q=0.8',
'Cache-Control:max-age=0',
'Connection:keep-alive',
'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36',
]);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
->
URL -> http://mhd2020.000webhostapp.com/ptmw/create_post.php?cat=1&usrnm=username&inifl=http://engnajjar2.000webhostapp.com/ptmw/work_fldr/POST.ini&cntntfl=http://engnajjar2.000webhostapp.com/ptmw/work_fldr/cntnt_fl.txt
<span id="post_ID" value="240">240</span><br>
<span id="sample-permalink_240" href="https://mhd2020.000webhostapp.com/?p=240" >https://mhd2020.000webhostapp.com/?p=240</span><br>
<span id="wp-admin-canonical_240" href="" ></span><br>
<span id="result_240">Successful</span><br>
URL -> http://engnajjar2.000webhostapp.com/ptmw/create_post.php?cat=1&usrnm=username&inifl=http://engnajjar2.000webhostapp.com/ptmw/work_fldr/POST.ini&cntntfl=http://engnajjar2.000webhostapp.com/ptmw/work_fldr/cntnt_fl.txt
<span id="post_ID" value="1639">1639</span><br>
<span id="sample-permalink_1639" href="https://engnajjar2.000webhostapp.com/2021/01/30/hello-39/" >https://engnajjar2.000webhostapp.com/2021/01/30/hello-39/</span><br>
<span id="wp-admin-canonical_1639" href="" ></span><br>
<span id="result_1639">Successful</span><br>
推荐阅读
- angular - 缩放客户端-JHipster
- html - 在元素的一半上设置背景
- javascript - JavaScript:如何访问 OS Windows 中的系统窗口
- java - Java正则表达式在拆分的url路径中查找所有单词
- django-rest-framework - django rest 框架 - 获取选择的 url
- java - 停止打印两次输出
- php - 带参数的 Laravel 电子邮件模板路由
- shopify - 在产品系列页面上显示色板(不是文本)Shopify
- firebase - 基于否定查询 Firestore 集合
- angular - 无法将数据传递给 MatTableDataSource