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

时间:2015-12-18 08:23:12

标签: php arrays url file-get-contents simple-html-dom

我试图找出如何返回数组中每个url的file_contents(urls_array)。到目前为止,下面的代码,使用simplehtmpdom只给出了一个结果,然后代码无法在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循环中获取所有网址标题

2 个答案:

答案 0 :(得分:1)

就像我在评论中所说的那样:根据事物的外观,问题的最可能原因是你在字符串上使用explode,使用逗号作为分隔符。但是,你的字符串也包含很多空格,你不会修剪它们。这可以解释为什么第一个URL没有错过,但第二个URL失败(该url以换行符开头)。

我建议您定义网址的数组而不是您爆炸的字符串,或者修剪所有网址:

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

这会为trim返回的数组中的每个值调用explode。但是,这有点傻。您开始对网址进行硬编码,那么为什么不编写数组而不是长字符串呢?

$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
);

答案 1 :(得分:1)

您收到此错误,因为您在阵列中有一些换行符。 当你执行数组的var_dump时,我得到:

$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'

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

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct linkedList
{
  char name[100];
  struct linkedList *next;
};

struct linkedList *head = NULL;


int main()
{
    struct linkedList *node1,*node2;

    node1 = (struct linkedList*)malloc(sizeof(struct linkedList));
    strcpy(node1->name, "aaa");
    node1->next = NULL;
    head = node1;

    node2 = (struct linkedList*)malloc(sizeof(struct linkedList));
    strcpy(node2->name, "bbb");
    node1->next = node2;
    node2->next = NULL;
    while(node1!=NULL)
    {
        printf("%s\n",node1->name);
        node1 = node1->next;
    }
    return 0;
 }

);