这是我的代码。 我想为$ new_ch []修改$ new_ch。 curl_init()这个函数可行吗?我想要一个每个$ new_ch都有一个自动增量的数组,这样我就对curl_init()这个函数的数组感到好奇。希望你回复。谢谢。
<?php
$count = 0;
$judge = fopen($sourcefile,"r+");
while(!feof($judge))
{
$destination = fgets($judge);
$new_ch = curl_init();
curl_setopt($new_ch, CURLOPT_URL, $destination);
curl_setopt($new_ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($new_ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($new_ch, CURLOPT_MAXREDIRS, 4);
curl_setopt($new_ch, CURLOPT_TIMEOUT, 30);
curl_setopt($new_ch, CURLOPT_USERAGENT, $agent_name);
curl_setopt($new_ch, CURLOPT_COOKIEFILE, $cookie_jar);
$getinner = curl_exec($new_ch);
$saveinner = fopen('./'.$datetime.'/'.$datetime.'.txt',"w+");
fwrite($saveinner,$getinner);
fclose($saveinner);
$dom_new = file_get_html('./'.$datetime.'/'.$datetime.'.txt');
foreach($dom_new->find('a') as $dom_new_element)
{
$needle_new = $dom_new_element->href;
$final_target = preg_match("/txt/",$needle_new);
$final_source = $site_rid.$needle_new."\n";
$mytrimaddress = parse_url($final_source, PHP_URL_QUERY);
$eachone = explode("&", $mytrimaddress);
$trimthename = trim($eachone[5]);
$lefttrim = ltrim($trimthename, "/");
$remaintrim = substr($lefttrim, 0, strlen($lefttrim)-1);
file_put_contents('./'.$datetime.'/'.$remaintrim, $final_source);
}
curl_close($new_ch);
$count++;
if($count > 5)
{
exit;
} else {
continue;
}
}
?>
编辑: 在过去,我不确定这样做是这样的。所以,我问了这个问题。我在互联网上搜索的大多数命令案例都被分配给变量而不是数组。
我希望对这些帖子增加一些投票。我也想帮助别人的问题。但是,目前的声誉很低,我无法回答任何问题。希望你理解我的忏悔。
答案 0 :(得分:0)
我不明白为什么这是不可能的。只需用数组替换$new_ch
:
$count = 0;
$curl_chs = [];
$judge = fopen($sourcefile,"r+");
while(!feof($judge))
{
$destination = fgets($judge);
$curl_chs[$count] = curl_init();
curl_setopt($curl_chs[$count], CURLOPT_URL, $destination);
curl_setopt($curl_chs[$count], CURLOPT_RETURNTRANSFER, true);
...etc
确保为该迭代中的所有curl
函数使用相同的数组索引。
注意:我正在使用$count
,因为它已经是一个从0
开始的计数器,并且在循环的每次迭代时都会递增。
答案 1 :(得分:0)
感谢。我在这里发布代码。
<?php
$datetime=date(“Ymd”);
$agent_name ='Mozilla/5.0 (Linux; Android 7.0; SM-G930VC Build/NRD90M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/58.0.3029.83 Mobile Safari/537.36';
$cookie_jar=‘/home/cookiefile.txt;
$judge = fopen($sourcefile,"r+");
while(!feof($judge))
{
$i=0;
$new_ch = array();
$destination = fgets($judge);
$new_ch[$i] = curl_init();
curl_setopt($new_ch[$i], CURLOPT_URL, $destination);
curl_setopt($new_ch[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($new_ch[$i], CURLOPT_FOLLOWLOCATION, true);
curl_setopt($new_ch[$i], CURLOPT_MAXREDIRS, 4);
curl_setopt($new_ch[$i], CURLOPT_TIMEOUT, 30);
curl_setopt($new_ch[$i], CURLOPT_USERAGENT, $agent_name);
curl_setopt($new_ch[$i], CURLOPT_COOKIEFILE, $cookie_jar);
$getinner = curl_exec($new_ch[$i]);
$saveinner = fopen('./'.$datetime.'/'.$datetime.'.txt',"w+");
fwrite($saveinner,$getinner);
fclose($saveinner);
$dom_new = file_get_html('./'.$datetime.'/'.$datetime.'.txt');
foreach($dom_new->find('a') as $dom_new_element)
{
$needle_new = $dom_new_element->href;
$final_target = preg_match("/txt/",$needle_new);
$final_source = $site_rid.$needle_new."\n";
$mytrimaddress = parse_url($final_source, PHP_URL_QUERY);
$eachone = explode("&", $mytrimaddress);
$trimthename = trim($eachone[5]);
$lefttrim = ltrim($trimthename, "/");
$remaintrim = substr($lefttrim, 0, strlen($lefttrim)-1);
file_put_contents('./'.$datetime.'/'.$remaintrim, $final_source);
}
}
$i++;
sleep(2);
}
?>
编辑:添加<?php MORE SOMETHING ?>
以完成代码内容给读者。我跳过了源目标以引起关注。如果你真的很好奇,我会发帖,请对这篇文章有很好的投票和评论。