-1

Possible Duplicate:
How to parse and process HTML with PHP?

i'm building my website with presentations off different products, and i face a few problems using curl basically what i need to do is to get some portions of html from different websites and display on my website ex: title, model, description, user reviews etc.... i managed to accomplish some of the code but when changing the source url stop working... even the source is the same my code:

$url = "http://www.tigerdirect.com/applications/SearchTools/item-details.asp?EdpNo=2819129&CatId=4938";

//$url = "http://www.tigerdirect.com/applications/SearchTools/item-details.asp?EdpNo=1808177&csid=_61"; //this one is not working....

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);

$source = curl_exec ($ch);

$start_description1 = "</tr>
</tbody>
</table>




<p>";
$end_description1 = "</div>
</div>
<div id=\"Videos\" style=\"display:inline;\">";
$description1_start_pos = strpos($source, $start_description1) + strlen($start_description1);
$description1_end_pos = strpos($source, $end_description1) - $description1_start_pos;
$description1 = substr($source, $description1_start_pos, $description1_end_pos);
echo $description1;

it works perfect but if i change the url it won't work... the problem is the start_description html code... on other pages the html code differs...

instead of:

</tr>
</tbody>
</table>




<p>

new page have:

</tr>
</tbody>
</table>


<p>

or:

</tr>
</tbody>
</table>

<p>

how can i avoid this error? or what to do to avoid cUrl errors, and retrieve the content i want ?

thank you!

Community
  • 1
  • 1
JDoe
  • 1
  • 1
  • 1
  • It is not within the set of features curl has to parse the HTML. You need a HTML parser which is *apart* from curl. Probably one of the duplicate questions is helping you? – hakre Aug 04 '12 at 17:24

1 Answers1

1

Instead of using strpos, you should parse the html and fetch the description from the html.

For this application, I recommend using the PHP Simple HTML DOM Parser.

Here is an example of how it works:

$html = file_get_html('http://www.tigerdirect.com/applications/SearchTools/item-details.asp?EdpNo=1808177&csid=_61');
//fetches html content from the url
$p = $html->find('p', 0);
//fetches the content of the first <p> element.

echo $p-> plaintext;

Hope this helps.

rickypai
  • 4,016
  • 6
  • 26
  • 30