fean
fean

Reputation: 546

how to read html body content using cURL

I'm using the following code to get response from a requested page using php

$ch = curl_init('http://myPageURL/');
curl_setopt($ch, CURLOPT_HEADER, 1);
$c = curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE); 

Here the response is showing header and other info including body content. but i only need the body content as a response, what would be the code to do it ?

thanks in advance

Upvotes: 3

Views: 18911

Answers (2)

blejzz
blejzz

Reputation: 3349

since you are only interested in the body tag you can do something like this:

  <?php
     $response = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
     $start = stripos($response, "<body");
     $end = stripos($response, "</body");

     $body = substr($response,$start,$end-$start);

  ?>

This is just a quick example how you can do this. But be aware that there can be several body tags in a page (if iframes are used). And also the body tag can contain attributes.

Upvotes: 2

Grego
Grego

Reputation: 2250

$ch = curl_init('http://myPageURL/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
$result = curl_exec($ch);

echo $result;

This will give the contents and I made it to add the content in the result variable and added some settings to make sure the content is received if the page you are going is redirecting to another.

Upvotes: 2

Related Questions