PHP error with getAttribute on a non object
Posted: Fri Apr 09, 2010 9:31 am
Hi,
I am new to PHP but have javascript experience. I am trying to use cUrl to scrape a page(google.com in this example) and extract a link. However I am getting the errors below:
Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /home/seriouss/public_html/PHP test.php on line 11
Fatal error: Call to a member function getAttribute() on a non-object in /home/seriouss/public_html/PHP test.php on line 30
the code I am using is below:
In the final script I will be using a more specific xPath to get a specific url from a different site but I am using this for testing. I have googled it a bit but nothing came up that seemed relevent or that I could make much sense of. The echo I am also just using for testing purposes and I will later use this URL further on in the script.
I am new to PHP but have javascript experience. I am trying to use cUrl to scrape a page(google.com in this example) and extract a link. However I am getting the errors below:
Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set in /home/seriouss/public_html/PHP test.php on line 11
Fatal error: Call to a member function getAttribute() on a non-object in /home/seriouss/public_html/PHP test.php on line 30
the code I am using is below:
Code: Select all
<?php
$target_url = "http://www.google.com/";
$userAgent = 'Firefox (WindowsXP) – Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6';
// make the cURL request to $target_url
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html= curl_exec($ch);
if (!$html) {
echo "<br />cURL error number:" .curl_errno($ch);
echo "<br />cURL error:" . curl_error($ch);
exit;
}
// parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);
// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
$href = $hrefs->item[0];
$url = $href->getAttribute('href');
echo $url
?>