views:

75

answers:

4

I want to know what is necessary to create a PHP script that can interact with a website like a normal browser. The website would be rich in Ajax, so the PHP script needs to know how to handle Javascript functions and maintain an continuous connection with the website.

+1  A: 

You're seeking what's called an HTTP client. PHP has one called cURL. See this example:

<?php

$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

cURL doesn't do Javascript however. If you need to evaluate Javascript, that's a far taller order and I might suggest you look at Google's V8 Javascript engine, but it's a C++ project.

cletus
Even though cURL is great, it doesn't do JavaScript.
Mikael S
+1  A: 

What you are looking for would amount to creating a full-fledged browser, complete with Javascript interpreter, in PHP. That is never, ever going to happen. You will have to split whatever you want to achieve into parts, and try to implement those in PHP.

Pekka
A: 

There's no way for PHP to inteperate Javascript function calls on a website. PHP runs in the background, Javascript runs in the foreground. They are completely different things.

Jessedc
A: 

If this is for testing purposes only you should maybe take a look at Selenium.

Cups