Php 存在多个url检查
我在一个数据库中有大约100k个URL,我想检查所有URL是否有效。我尝试使用PHP和curl,但速度非常慢,并导致脚本超时。有没有更好的方法使用其他shell脚本来实现这一点 到目前为止,我试过:Php 存在多个url检查,php,shell,curl,Php,Shell,Curl,我在一个数据库中有大约100k个URL,我想检查所有URL是否有效。我尝试使用PHP和curl,但速度非常慢,并导致脚本超时。有没有更好的方法使用其他shell脚本来实现这一点 到目前为止,我试过: // By default get_headers uses a GET request to fetch the headers. If you // want to send a HEAD request instead, you can do so using a stream context
// By default get_headers uses a GET request to fetch the headers. If you
// want to send a HEAD request instead, you can do so using a stream context:
stream_context_set_default(
array(
'http' => array(
'method' => 'HEAD'
)
)
);
$headers = get_headers('http://example.com');
它正在循环运行 您可以使用mechanize python模块访问网站并从中获取响应服务器响应中存在大量延迟,因此此问题有助于并行化。尝试将列表拆分为多个子列表,并并行运行脚本,每个脚本处理不同的列表 尝试查看
split
命令以生成列表
所以,你会得到这样的结果:
#!/bin/bash
split -l 1000 urllist.txt tmpurl # split bigfile into 1000 line subfiles called tmpurl*
for p in tmpurl* # for all tmpurl* files
do
# Start a process to check the URLs in that list
echo start checking file $p in background &
done
wait # till all are finished
在我放置“在后台开始检查文件$p”的地方,您需要提供一个简单的PHP或shell脚本,该脚本将文件名作为参数(或从其stdin读取),并对文件中的所有URL进行for循环检查,但您已经在这样做了
额外信息:
为了好玩,我用curl-I-s
列出了1000个URL和每个URL的curl
ed头。在连续案例中,耗时4分19秒。当我使用上面的脚本将1000个URL拆分为每个文件中100个URL的子列表并启动10个进程时,整个测试耗时22秒,因此速度提高了12倍。将列表拆分为50个URL的子列表,导致20个进程在14秒内全部完成。因此,正如我所说,这个问题很容易并行。我的bash解决方案:
#!/bin/bash
###############################################
# mailto: ggerman@gmail.com
# checkurls
# https://github.com/ggerman/checkurls
# require curl
###############################################
url() {
cat urls.csv |
replace |
show
}
replace() {
tr ',' ' '
}
show() {
awk '{print $1}'
}
url | \
while read CMD; do
echo $CMD
curl -Is $CMD | head -n 1
done
您的意思是创建多个通过不同url插槽的脚本?是的,请参阅更新的答案。这将创建100个进程,每个进程将使用您的数字检查1000个URL。