Possible Duplicate:
Is it possible to use curl with relative path in PHP?
I have been trying for hours to get a script to work that will follow relative urls.
A quick summary of the problem.
I'm using the FOLLOWLOCATION
option to follow URLs, but when the url is:
/redirectPage.html
curl follows it like this:
localhost/redirectPage.html
Here's my code:
$ch = curl_init();
//set options
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, 'CURL_HTTP_VERSION_1_1');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$content = curl_exec($ch);
curl_close($ch);
The few "solutions" I've found involve parsing the DOM Document and replacing the href attributes with the absolute URL, but I'm looking for a solution that will allow me to still use FOLLOWLOCATION
. Is there a way to set a base URL in curl, or capture the redirected url that is followed in the FOLLOWLOCATION
option and append a string to it?
Please help.
FOLLOWLOCATION
allows cURL
to follow Location: /redirects
in the response headers. But does not allow you to download relative URLs.
You need to resolve the URL yourself. To resolve a relative URL, you need an absolute one to use as reference. But where do you provide it to cURL? You don't, you can't, you shouldn't.
It's you job to sanitize and resolve URLs. Not the HTTP Client's. cURL resolves relative Location: headers
. But not relative URL requests based on some previous memory of a page that might have never existed.