PHP的函数curl_init()变量作为数组变量(已解决。已了解将数组用于curl_curl()。) [英] PHP's function curl_init() variable as array variable (Solved. Had understood about using an array to curl_curl().)
问题描述
这是我的代码。
我想为$ new_ch []修改$ new_ch。 curl_init()这个函数可行吗?我想要一个每个$ new_ch具有自动增量的数组,以便我对带有curl_init()这个函数的数组感到好奇。希望你回复。谢谢。
Here is my code. I want to modify the $new_ch for $new_ch[]. Is it viable for curl_init() this function? I want an array with an auto increment for each $new_ch so that I am curious about the array with curl_init() this function. Wish you reply. Thanks.
<?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;
}
}
?>
编辑:
在过去的时刻,我不确定采用这种方式。所以,我问了一个问题。我在Internet上搜索的大多数命令案例都分配给变量而不是数组。
At the past moment, I am not sure this way to do like this. So, I asked the question. Most of the command cases I searched on internet are assigned to a variable instead of an array.
我希望对这些帖子增加一些投票。我也想帮助别人的问题。但是,当前的声誉非常低,我无法回答任何问题。希望您理解我的表白。
I wish some added votes for those post. I also like to help others' question. But, the current reputation is quite low and I am not able to reply any question. Hope you understand my confession.
推荐答案
我不明白为什么这不可能。只需将您的 $ new_ch
替换为一个数组:
I don't see why this wouldn't be possible. Just replace your $new_ch
with an array:
$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
请确保对所有<$使用相同的数组索引c $ c> curl 函数在该迭代中。
Just make sure you use the same array index for all curl
functions in that iteration.
注意:我使用的是 $ count
,因为它已经是一个从 0
开始的计数器,并在循环的每次迭代中递增。
Note: I'm using $count
because it's already a counter that starts at 0
and is incremented on every iteration of your loop.
这篇关于PHP的函数curl_init()变量作为数组变量(已解决。已了解将数组用于curl_curl()。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!