dongzhu0327 2012-11-01 19:33
浏览 192

php curl - 跟随相对路径[重复]

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.

  • 写回答

1条回答 默认 最新

  • dongxichan8627 2012-11-01 19:47
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题