Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

curl - PHP's function curl_init() variable as array variable (Solved. Had understood about using an array to curl_curl().)

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."
";
    $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;
  }
}
?>

Edit: 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Just make sure you use the same array index for all curl functions in that iteration.

Note: I'm using $count because it's already a counter that starts at 0 and is incremented on every iteration of your loop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...