Curl返回400错误的请求(带空格的URL) [英] Curl returns 400 bad request (url with spaces)

查看:569
本文介绍了Curl返回400错误的请求(带空格的URL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用curl库并尝试从url获取图像时,出现400错误的请求错误. 我发现问题在于编码网址. 但是在我的情况下,这是行不通的,因为我的网址-它是服务器端图像的路径-如

When i use curl library and try to get image from url i get 400 bad request error. I founded that problem is with encoding url. But in my case it's not work, because my url - it's path to image on server side - like

http://example.com/images/products/product 1.jpg

我知道用户名文件中的空格是一种不好的做法,但这不是我的服务器,也不是我创建的那些文件.

I understand that user spaces in name files it's bad practice, but it's not my server and not i created those files.

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, urlencode($url));
echo $ret = curl_exec($ch);

当我使用urlencode函数时-curl返回http_code = 0

When i use urlencode function - curl return http_code = 0

已更新

$url = str_replace(' ', '+', $url);

不起作用,服务器返回404错误.

推荐答案

这可行吗?

$url = 'http://host/a b.img';
$url = str_replace(" ", '%20', $url);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
echo $ret = curl_exec($ch);

这篇关于Curl返回400错误的请求(带空格的URL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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