如何连接每个数组中的每个url

时间:2014-04-05 15:51:37

标签: php arrays

我正在使用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;     
    }

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

1 个答案:

答案 0 :(得分:0)

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

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

  // do something with $elementHtml
}