使用PHP cURL下载带有特殊字符的URL [英] Using PHP cURL to downloading URL with special characters

查看:180
本文介绍了使用PHP cURL下载带有特殊字符的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下载以下URL https://www.astegiudiziarie.it/vendita-asta-appartamento-genova-via-san-giovanni-d'acri-14-1360824 与PHP cURL:

I'm trying to download the following URL https://www.astegiudiziarie.it/vendita-asta-appartamento-genova-via-san-giovanni-d’acri-14-1360824 with PHP cURL:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.astegiudiziarie.it/vendita-asta-appartamento-genova-via-san-giovanni-d’acri-14-1360824');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$f = curl_exec($ch);
curl_close($ch);
echo $f;

,但是服务器始终返回错误页面。在Web浏览器中导航相同的URL效果很好。手动将 curl_exec 返回的HTML源与Web浏览器中的HTML源进行比较,即可立即看到差异。

but the server always returns an error page. Navigating the same URL in a web browser works fine. Manually comparing the HTML source returned by curl_exec with the HTML source in a web browser, the difference is immediately noticeable.

我试图 utf8_decode() URL没有成功。

I tried to utf8_decode() the URL without success.

我不能简单地包装URL
urlencode()中,因为它甚至可以对 /

I cannot simply wrap the url in urlencode() because it will encode even normal characters like : and /.

以编程方式检索(抓取)URL,并且它们的结构不一定总是相同的,因此很难将其拆分并使用urlencode

That URLs are retrieved programmatically (scraping) and won't always have the same structure, so it would be difficult to split them and urlencode just some parts.

顺便说一句,现代Web浏览器似乎很好地处理了这种情况。在PHP中有解决方案吗?

Btw, it seems that modern web browsers handle this case very well. There is a solution for that in PHP?

推荐答案

我最终得到了:

function urlencode_parts($url) {
    $parts = parse_url($url);
    $parts['path'] = implode('/', array_map('urlencode', explode('/', $parts['path'])));
    $url = new \http\Url($parts);
    return $url->toString();
}

使用软件包 \http\Url ,它替换了最新PHP版本中的 http_build_url 函数。

using the package \http\Url, that replaces http_build_url function in newest PHP versions.

似乎 file_get_contents 不能与特殊字符一起使用。

Seems that file_get_contents doesn't work too with special characters.

更新2018-05-09 :似乎已在cUrl 7.52.1中修复

Update 2018-05-09: it seems fixed in cUrl 7.52.1

这篇关于使用PHP cURL下载带有特殊字符的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆