Reputation: 101
I was using file_get_content like this earlier
if($html = @DOMDocument::loadHTML(file_get_contents($url))) {.. }
but switching to curl as it's more secure, but I got error
curl_exec() expects parameter 1, sting is given
My code
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$html = curl_exec($url);
curl_close($ch);
if($html) {
$xpath = new DOMXPath($html);
..
..
}
Upvotes: 1
Views: 4053
Reputation: 4275
The error is saying that you are giving string to curl_exec
, give it the curl handle. Use the code below
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$html = curl_exec($ch);
curl_close($ch);
if($html) {
$xpath = new DOMXPath($html);
..
..
}
Hope this helps you
Upvotes: 1
Reputation: 9034
You are executing the URL string instead of the curl handle
$html = curl_exec($url);
change to
$html = curl_exec($ch);
Upvotes: 3