1

I'm working on my PHP to search for the elements. There are are one element called <p id='links'>, I use simple_html_dom method to parsing the contains from my script called get-listing.php.

Here is the example output from get-listing.php:

<p id='channels'>101 ABC FAMILY</p>
<p id='links'>
    <a href='http://www.mysite.com/get-listing.php?channels=ABC FAMILY&id=101'>http://www.mysite.com/get-listing.php?channels=ABC FAMILY&id=101</a>
</p>
<a id="aTest" href="">Stream 1</a>
<p id='channels'>102 CBS</p>
<p id='links'>
    <a href='http://www.mysite.com/get-listing.php?channels=CBS&id=102'>http://www.mysite.com/get-listing.php?channels=CBS&id=102</a>
</p>
<a id="aTest" href="">Stream 1</a>
<p id='channels'>103 CNN USA</p>
<p id='links'>
    <a href='http://www.mysite.com/get-listing.php?channels=CNN USA&id=103'>http://www.mysite.com/get-listing.php?channels=CNN USA&id=103</a>
</p>
<a id="aTest" href="">Stream 1</a>
<p id='channels'>105 ESPN USA</p>
<p id='links'>
    <a href='http://www.mysite.com/get-listing.php?channels=ESPN USA&id=105'>http://www.mysite.com/get-listing.php?channels=ESPN USA&id=105</a>
</p>
<a id="aTest" href="rtmp://$OPT:rtmp-raw=rtmp://ny.iguide.to/edge playpath=49f5xnbs2wra0ut swfUrl=http://player.ilive.to/player_ilive_2.swf pageUrl=http://www.ilive.to token=UYDk93k#09sdafjJDHJKAD873">Stream 1</a>
<p id='channels'>106 FOX News</p>
<p id='links'>
    <a href='http://www.mysite.com/get-listing.php?channels=FOX News&id=106'>http://www.mysite.com/get-listing.php?channels=FOX News&id=106</a>
</p>
<a id="aTest" href="">Stream 1</a>
<p id='channels'>107 Animal Planet</p>
<p id='links'>
    <a href='http://www.mysite.com/get-listing.php?channels=Animal Planet&id=107'>http://www.mysite.com/get-listing.php?channels=Animal Planet&id=107</a>
</p>
<a id="aTest" href="">Stream 1</a>
<p id='channels'>108 USA Network</p>
<p id='links'>
    <a href='http://www.mysite.com/get-listing.php?channels=USA Network&id=108'>http://www.mysite.com/get-listing.php?channels=USA Network&id=108</a>
</p>
<a id="aTest" href="">Stream 1</a>

Here is my PHP script:

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

$countp = $html->find('p');     
header("Content-type: text/xml");
$xml .= "<?xml version='1.0' encoding='UTF-8' ?>";
//echo $xml;
$xml .= '<tv generator-info-name="www.testbox.elementfx.com/xmltv">';
?>

I want to create the loops to get the url in each array from get-listing.php with one element id=links.

Can you please tell me how I can do that?

Anonymous
  • 11,748
  • 6
  • 35
  • 57

1 Answers1

0

Assuming simple_html_dom.php gets your data as described here http://simplehtmldom.sourceforge.net/ then you should be able to use

foreach to go through the results

$links = $html->find('p[id=links] a');
foreach ($links as $link) {
     //Get raw URL's here
     $urls[] = $link->href;
}

EDIT

if you want to sort through the hrefs you could do a few simple tests here

foreach ($links as $link) {
  //Get raw URL's here
  if (strstr($link->href,'get_listing')) {
     $listings[] = $link->href;
  } else {
     $general[] = $link->href;
  }
} 
dops
  • 800
  • 9
  • 17
  • thank you very much for this, do you know how I can open for each url in each array? –  Apr 05 '14 at 16:12
  • @user3494862 Sorry, I'm not sure what you mean by that, could you eloborate? – dops Apr 05 '14 at 16:44
  • well I meant that when I get the list of urls, I want to connect each of them in each array. e.g: I connect to get-listing.php and I connect to `http://testbox.elementfx.com/get-listing.php?channels=ABC%20FAMILY&id=101`, `http://testbox.elementfx.com/get-listing.php?channels=CBS&id=102`, `http://testbox.elementfx.com/get-listing.php?channels=CNN%20USA&id=103` and so on. How I can connect to each url for each array when I get the list of urls? –  Apr 05 '14 at 17:21
  • I would walk through the array looking for matches using $listingsArray = array_walk($fruits, 'extractURLType','get_listing'); and defining a function extractURLType($data,$key,$text) where you match the $text to the $data, see here for the manual entry http://uk3.php.net/array_walk – dops Apr 06 '14 at 02:13
  • there are already matches, but i need to send request to each url to get the responses. do you know how i can send request to each url to get the list of elements in each response? –  Apr 06 '14 at 02:16
  • I've edited my answer to show you a way, there are better ways using array_walk or http://uk3.php.net/manual/en/function.preg-grep.php – dops Apr 06 '14 at 02:20
  • thank you very much for this, i have a problem with send request to each url. I'm getting an warning with file_get_contents. here is the warning: `Warning: file_get_contents('http://www.mysite.com/get-listing.php?channels=ABC FAMILY&id=101'>): failed to open stream: No such file or directory in /home/myusername/public_html/simple_html_dom.php on line 78`. Do you have any idea why i got the warning? –  Apr 06 '14 at 13:49
  • Probably the same issue as here http://stackoverflow.com/questions/17363545/file-get-contents-is-not-working-for-some-url – dops Apr 07 '14 at 13:40