如何从 urls 数组的每个 url 中获取 file_contents

How to get file_contents from each url of an urls array

我想弄清楚如何 return 数组中每个 url 的 file_contents (urls_array)。到目前为止,使用 simplehtmpdom 的以下代码只给我一个结果,然后代码无法 运行 on.....在 foreach 循环中。

$urlsall = 'http://php.net,
http://php.net/downloads,
http://php.net/docs.php,
http://php.net/get-involved,
http://php.net/support,
http://php.net/manual/en/getting-started.php,
http://php.net/manual/en/introduction.php,
http://php.net/manual/en/tutorial.php,
http://php.net/manual/en/langref.php,
http://php.net/manual/en/language.basic-syntax.php,
http://php.net/manual/en/language.types.php,
http://php.net/manual/en/language.variables.php,
http://php.net/manual/en/language.constants.php,
http://php.net/manual/en/language.expressions.php,
http://php.net/manual/en/language.operators.php,
http://php.net/manual/en/language.control-structures.php,
http://php.net/manual/en/language.functions.php,
http://php.net/manual/en/language.oop5.php,
http://php.net/manual/en/language.namespaces.php,
http://php.net/manual/en/language.errors.php,
http://php.net/manual/en/language.exceptions.php,
http://php.net/manual/en/language.generators.php,
http://php.net/manual/en/language.references.php,
http://php.net/manual/en/reserved.variables.php,
http://php.net/manual/en/reserved.exceptions.php,
http://php.net/manual/en/reserved.interfaces.php,
http://php.net/manual/en/context.php';

$urls_array = explode(',', $urlsall);
//var_dump ($urls_array);
foreach ($urls_array as $url)
    {

         $html = SimpleHtmlDom::file_get_html($url); 
         $title = $html->find('title',0);
        echo $title->plaintext; 

    }

结果:PHP:超文本预处理器

ERROR: An error occured, The error has been reported.
Error on Dec 18, 2015 17:16PM - file_get_contents( http://php.net/downloads): failed to open stream: Invalid argument in E:\xampp\htdocs\sitename\SimpleHtmlDom.php on line 81

我想要做的是从上面的 foreach 循环中获取所有 url 的标题

就像我在评论中所说的那样:从外观上看,问题的最可能原因是您在字符串上使用 explode,并使用逗号作为分隔符。但是,您的字符串也包含很多空格,您没有 trimming。这可以解释为什么第一个 URL 无误通过,但第二个失败(url 以换行符开头)。

我建议您定义一个 url 的 数组 而不是您展开的字符串,或者您 trim 所有 urls:

$urls = array_map('trim', explode(',', $urlsall));

这会为数组中 explode returns 的每个值调用 trim。然而,这有点傻。您正在对 url 进行硬编码,那么为什么不写一个数组而不是一个长字符串呢?

$urls = array(
    'http://php.net',
    'http://php.net/downloads',
    'http://php.net/docs.php',
    'http://php.net/get-involved',
    'http://php.net/support',
    'http://php.net/manual/en/getting-started.php',
    //rest of the urls here
);

你得到这个错误是因为你的数组中有一些换行符。 当您对数组执行 var_dump 时,我得到:

array (size=27)
   0 => string 'http://php.net' (length=14)
   1 => string '
      http://php.net/downloads' (length=26)
   2 => string '
       http://php.net/docs.php' (length=25)
   3 => string '
       http://php.net/get-involved' (length=29)

你为什么使用爆炸? 直接创建一个数组来执行此操作:

$urlsall = array(
'http://php.net',
'http://php.net/downloads',
'http://php.net/docs.php',
'http://php.net/get-involved',
'http://php.net/support',
'http://php.net/manual/en/getting-started.php',
'http://php.net/manual/en/introduction.php',
'http://php.net/manual/en/tutorial.php',
'http://php.net/manual/en/langref.php',
'http://php.net/manual/en/language.basic-syntax.php',
'http://php.net/manual/en/language.types.php',
'http://php.net/manual/en/language.variables.php',
'http://php.net/manual/en/language.constants.php',
'http://php.net/manual/en/language.expressions.php',
'http://php.net/manual/en/language.operators.php',
'http://php.net/manual/en/language.control-structures.php',
'http://php.net/manual/en/language.functions.php',
'http://php.net/manual/en/language.oop5.php',
'http://php.net/manual/en/language.namespaces.php',
'http://php.net/manual/en/language.errors.php',
'http://php.net/manual/en/language.exceptions.php',
'http://php.net/manual/en/language.generators.php',
'http://php.net/manual/en/language.references.php',
'http://php.net/manual/en/reserved.variables.php',
'http://php.net/manual/en/reserved.exceptions.php',
'http://php.net/manual/en/reserved.interfaces.php',
'http://php.net/manual/en/context.php'

);