【问题标题】:PHP's function curl_init() variable as array variable (Solved. Had understood about using an array to curl_curl().)PHP 函数 curl_init() 变量作为数组变量(已解决。了解使用数组来 curl curl()。)
【发布时间】:2018-02-13 22:22:04
【问题描述】:

这是我的代码。 我想为 $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;
  }
}
?>

编辑: 在过去的时刻,我不确定这种方式可以这样做。所以,我问了这个问题。我在互联网上搜索的大多数命令案例都分配给了一个变量而不是一个数组。

我希望为这些帖子增加一些投票。我也喜欢帮助别人的问题。但是,目前的声誉很低,我无法回答任何问题。希望你能理解我的表白。

【问题讨论】:

  • 您的问题非常不清楚且难以理解。
  • 你为什么不尝试一下,当它不起作用时回来显示你的损坏代码。
  • 您好,您需要了解的模糊部分在哪里?我想修改。
  • “curl_init() 的变量数组”是什么意思curl_init() 在成功时返回 cURL 句柄,在错误时返回 false。像已经提到的其他人一样,如果您有一些特定问题,请尝试并回来。
  • @MagnusEriksson 谢谢。

标签: php curl


【解决方案1】:

我不明白为什么这是不可能的。只需将 $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 开始的计数器,并且在循环的每次迭代中都会递增。

【讨论】:

  • 你的解释很简短,切中要害。这个真正的帮助和突破点指向我过去不假思索和尝试的问题。谢谢。祝声望提高一些选票。谢谢。
【解决方案2】:

谢谢。我在这里发布代码。

<?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);
}
?>

编辑:添加&lt;?php MORE SOMETHING ?&gt; 以完成代码内容给读者。我跳过源目标以引起关注。如果您真的对此感到好奇,我会发布它,并请为这篇文章投好票和 cmets。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 2011-01-11
    • 1970-01-01
    相关资源
    最近更新 更多