doupuchen6378 2016-10-31 15:32
浏览 67
已采纳

编码动态生成的URL的参数,以便在CURL中使用

I write a robot and fetch all URL in web page. the URLs is be like this:

http://www.example.com/result/رایانه

Now, When I try to fetch content of this url by CURL, give me this error:

400 Bad request

I know this cause because for "رایانه" in url and must encoding it.
But that URL is dynamically, and I need a solution for encode just params in URL.
Be like this:

"http://www.example.com/result/" . urlencode("رایانه")

Or another example:

Maybe I have this URL:

http://www.example.com/result/سوتی/?foo=علی&bar=حسن

If I use urlencode() return this:

http%3A%2F%2Fwww.example.com%2Fresult%2F%D8%B3%D9%88%D8%AA%DB%8C%2F%3Ffoo%3D%D8%B9%D9%84%DB%8C%26bar%3D%D8%AD%D8%B3%D9%86

But so must just encode this words: سوتی, علی, حسن.
and have this correct encoded:

http://www.example.com/result/%D8%B3%D9%88%D8%AA%DB%8C/?foo=%D8%B9%D9%84%DB%8C&bar=%D8%AD%D8%B3%D9%86

I need this for use in CURL.
How can I do this?

EDIT:

I found this code:

echo implode("/", array_map("urlencode", explode("/", $string)));

return:

http%3A//www.example.com/result/%D8%B3%D9%88%D8%AA%DB%8C/%3Ffoo%3D%D8%B9%D9%84%DB%8C%26bar%3D%D8%AD%D8%B3%D9%86

But the result is not exactly true.

  • 写回答

1条回答 默认 最新

  • duanniubeng2265 2016-10-31 17:39
    关注

    I find a solution:

    $string = 'http://www.example.com/result/سوتی/?foo=علی&bar=حسن';
    $string = urlencode($string);
    echo str_replace(array('%3A', '%2F', '%3F', '%3D', '%26'), array(':', '/', '?', '=', '&'), $string);
    

    Output:

    http://www.example.com/result/%D8%B3%D9%88%D8%AA%DB%8C/?foo=%D8%B9%D9%84%DB%8C&bar=%D8%AD%D8%B3%D9%86
    

    It work with any URL and accept for use in CURL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?