Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 结合curl multi和strpos()查找网页上的文本_Php_Curl_Curl Multi - Fatal编程技术网

Php 结合curl multi和strpos()查找网页上的文本

Php 结合curl multi和strpos()查找网页上的文本,php,curl,curl-multi,Php,Curl,Curl Multi,编辑 我试图使用curl multi检查来自网站的响应,并另外检查每个curl响应中的一部分文本。我已将数据分组到一个数组中,但我无法确定是否使用了正确/最有效的方法来使用“post”文本运行strpos()函数 $data = array(array()); $data[0]['url'] = 'http://www.google.com'; $data[0]['post'] = 'google text'; $data[1]['url'] = 'http://www.yahoo.co

编辑

我试图使用curl multi检查来自网站的响应,并另外检查每个curl响应中的一部分文本。我已将数据分组到一个数组中,但我无法确定是否使用了正确/最有效的方法来使用“post”文本运行strpos()函数

$data = array(array());

$data[0]['url']  = 'http://www.google.com';
$data[0]['post'] = 'google text';

$data[1]['url']  = 'http://www.yahoo.com';
$data[1]['post'] = 'yahoo text';

$r = multiRequest($data);

echo '<pre>';
print_r($r);
有人能建议我的解决方案是否合适吗?是否有更有效/更好的方法来执行strpos()检查


谢谢

使用数组,以便将所有URL、字符串和卷曲句柄关联在一起:


然后在处理结果时执行类似的循环。

您能建议我的解决方案是否合适吗?是否有更有效/更好的方法来执行strpos()检查?
function multiRequest($data, $options = array()) {

  // array of curl handles
  $curly = array();

  // data to be returned
  $result = array();

  // multi handle
  $mh = curl_multi_init();

  // loop through $data and create curl handles
  // then add them to the multi-handle
  foreach ($data as $id => $d) {

    $curly[$id] = curl_init();

    $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
    curl_setopt($curly[$id], CURLOPT_URL,            $url);
    curl_setopt($curly[$id], CURLOPT_HEADER,         0);
    curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curly[$id], CURLOPT_USERAGENT,      'Chrome: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2');

    // extra options?
    if (!empty($options)) {
      curl_setopt_array($curly[$id], $options);
    }

    curl_multi_add_handle($mh, $curly[$id]);
  }

  // execute the handles
  $running = null;
  do {
    curl_multi_exec($mh, $running);
  } while($running > 0);

  // get content and remove handles
  foreach($curly as $id => $c) {
    $result[$id][] = curl_getinfo($c, CURLINFO_EFFECTIVE_URL);
    $result[$id][] = curl_getinfo($c, CURLINFO_HTTP_CODE);
    $result[$id][] = curl_getinfo($c, CURLINFO_CONTENT_TYPE);
    $url = curl_getinfo($c, CURLINFO_EFFECTIVE_URL);
    // loop data again
    foreach ($data as $id => $d){
        if($url==$d['url']){ // only check current url data
            $text = curl_exec($c);
        $result[$id][] = strpos($text, $d['post']); 
        }
    }
    curl_multi_remove_handle($mh, $c);
  }

  // all done
  curl_multi_close($mh);

  return $result;
}
$stuff = array(
   0 => array('url' => 'google', 'text' => 'googletext', 'curl' => null)
   1 => array('url' => 'yahoo', 'text' => 'yahootext', 'curl' => null)
   etc..
);

foreach($stuff as $key => $info) {
   $stuff[$key]['curl'] =  curl_init($stuff[$key]['url']);
   curl_multi_add_handle($mh, $stuff[$key]['curl']);   
}