【问题标题】:PHP encoding URL with cURL带有 cURL 的 PHP 编码 URL
【发布时间】:2014-06-12 06:21:11
【问题描述】:

我正在尝试使用 cURL 访问一组 URL。部分链接如下所示:

 http://somelink.com/directory/file name.php
 http://somelink.com/file name.php
 http://somelink.com/(some file).txt
 http://somelink.com/directory/random chars &^%.txt
 http://somelink.com/!@#$%^&*.txt

只是一些带有一些随机字符的文件。我知道有 urlencode() 函数,但是这个函数还对每个目录后的 http:// 和 / 进行 url 编码,这将不允许页面在 cURL 中正确加载。是否有一些正则表达式可用于分隔每个 / 之后的单词并单独对其进行编码,但忽略根目录 / 之前所有内容的编码?

【问题讨论】:

    标签: php regex curl encoding


    【解决方案1】:

    您可以编写一个函数以您拥有的格式(数组)接受输入并以所需的格式输出,如下所示:

    function urlencode_array($url_array)
    {
        $encoded = array();
    
        foreach ($url_array as $url) {
            $encoded[] = "http://" . str_replace("%2F", "/", rawurlencode(substr($url, 7)));
        }
    
        return $encoded;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-25
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多