【问题标题】:how to connect each url in each array如何连接每个数组中的每个 url
【发布时间】:2014-05-17 23:11:04
【问题描述】:

我正在使用我的 PHP 来获取每个数组中的元素列表。

这是元素的输出:

<?xml version='1.0' encoding='UTF-8' ?>
<tv generator-info-name="www.mysite.com/test">
<p id='links'>http://www.mysite.com/get-listing.php?channels=ABC FAMILY&id=101</p>
<p id='links'>http://www.mysite.com/get-listing.php?channels=CBS&id=102></p>
<p id='links'>http://www.mysite.com/get-listing.php?channels=CNN USA&id=103></p>
<p id='links'>http://www.mysite.com/get-listing.php?channels=ESPN USA&id=105></p>
<p id='links'>http://www.mysite.com/get-listing.php?channels=Fox News&id=106></p>
<p id='links'>http://www.mysite.com/get-listing.php?channels=Animal Planet&id=107></p>

这里是 PHP:

<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = false;
$link;
include ('simple_html_dom.php');

$base1 = "http://www.mysite.com/get-listing.php";
$html = file_get_html($base1);      

$xml .= "<?xml version='1.0' encoding='UTF-8' ?>";
//echo $xml;
$xml .= '
<tv generator-info-name="www.testbox.elementfx.com/test">';
echo $xml;

foreach($html->find('p[id=links]') as $element)
{
  echo $element;
  //open each url 
}
?>

当我得到每个数组的元素列表时,你知道如何使用 simple_html_dom 为每个数组使用一个元素连接到每个 url?

编辑:当我尝试这个时:

    foreach($html->find('p[id=links]') as $element)
    {
      //echo $element;
      //open each url
      $elementhtml = $element;
      // do something with $elementHtml
      //echo $elementhtml;
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $elementhtml);
      curl_setopt($ch, CURLOPT_HEADER, 1);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
      $result = curl_exec($ch);
      curl_close($ch);  
      echo $result;     
    }

它不会向每个 url 发送请求。有什么想法吗?

【问题讨论】:

  • 有谁知道我如何使用 simple_html_dom 连接到每个数组中的每个 url?
  • 连接是什么意思?是否要向每个 url 发送 http 请求?
  • @AndyLibrian 是的,我当然愿意。我想使用 simple_html_dom 向每个 url 发送请求。我该怎么做?

标签: php arrays


【解决方案1】:
foreach($html->find('p[id=links]') as $element)
{
  echo $element;
  //open each url 

  $elementHtml = file_get_html($element->innertext);

  // do something with $elementHtml
}

【讨论】:

  • 非常感谢,我在向每个 url 发送请求时遇到问题。我收到有关 file_get_contents 的警告。这是警告:Warning: file_get_contents('http://www.mysite.com/get-listing.php?channels=ABC FAMILY&amp;id=101'&gt;): failed to open stream: No such file or directory in /home/myusername/public_html/simple_html_dom.php on line 78。你知道我为什么收到警告吗?
  • 我试过了,但它不起作用。它不会让我向每个 url 发送请求。你知道为什么吗?我已经将allow_url_fopen=1 放入我的 php.ini 中。
  • 更改php.ini后确定重启了网络服务器?
  • 是的,我做到了。一切正常,但我想我的代码一定是错误的。请在我的第一篇文章中看到我更新了代码。
猜你喜欢
  • 2019-09-16
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 2017-05-26
  • 1970-01-01
  • 2017-10-14
  • 2021-12-30
  • 1970-01-01
相关资源
最近更新 更多