GoldenDays
GoldenDays

Reputation: 23

How to collect HTML source response from a remote server?

From within the HTML code in one of my server pages I need to address a search of a specific item on a database placed in another remote server that I don’t own myself.

Example of the search type that performs my request: http://www.remoteserver.com/items/search.php?search_size=XXL

The remote server provides to me - as client - the response displaying a page with several items that match my search criteria.

I don’t want to have this page displayed. What I want is to collect into a string (or local file) the full contents of the remote server HTML response (the code we have access when we click on ‘View Source’ in my IE browser client).

If I collect that data (it could easily reach reach 50000 bytes) I can then filter the one in which I am interested (substrings) and assemble a new request to the remote server for only one of the specific items in the response provided.

Is there any way through which I can get HTML from the response provided by the remote server with Javascript or PHP, and also avoid the display of the response in the browser itself?

I hope I have not confused your minds … Thanks for any help you may provide.

Upvotes: 2

Views: 1107

Answers (1)

user895378
user895378

Reputation:

As @mario mentioned, there are several different ways to do it.

Using file_get_contents():

$txt = file_get_contents('http://www.example.com/');
echo $txt;

Using php's curl functions:

$url = 'http://www.mysite.com';
$ch = curl_init($url);

// Tell curl_exec to return the text instead of sending it to STDOUT
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

// Don't include return header in output
curl_setopt($ch, CURLOPT_HEADER, 0);

$txt = curl_exec($ch);
curl_close($ch);

echo $txt;

curl is probably the most robust option because you have options for more control over the exact request parameters and possibilities for error handling when things don't go as planned

Upvotes: 6

Related Questions