如果URL包含特殊字符,则CURL不返回任何内容

时间:2014-04-24 09:39:40

标签: php curl

我想使用CURL从网址获取详细信息,它可以正常使用普通字符网址。但其中一个amy URL包含特殊字符,如“ó”。

我的网址示例如下:

http://www.abc.com/aóvek-xyz-pqr/1/8/

我的PHP代码是:

 $ch = curl_init($my_url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
 $content = curl_exec($ch);

3 个答案:

答案 0 :(得分:2)

尝试使用以下内容(取自urlencode only the directory and file names of a URL):

$encoded_url = preg_replace_callback('#://([^/]+)/([^?]+)#', function ($match) {
                return '://' . $match[1] . '/' . join('/', array_map('rawurlencode', explode('/', $match[2])));
            }, $my_url);

$ch = curl_init($encoded_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);

答案 1 :(得分:1)

http中的URL只能使用一组特定字符构建。对于其他每个人,你需要对它们进行百分比编码。

http://en.wikipedia.org/wiki/Percent-encoding

在PHP中,您可以使用urlencode

http://php.net/manual/en/function.urlencode.php

您的代码应如下所示

 $ch = curl_init(urlencode($my_url));
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
 $content = curl_exec($ch);

答案 2 :(得分:0)

试试这个

function get_web_page( $url )
    {               
            $options = array(
            CURLOPT_RETURNTRANSFER  => true, 
            CURLOPT_HEADER          => false,
            CURLOPT_FOLLOWLOCATION  => true, 
        );

        $ch      = curl_init( $url );
        curl_setopt_array( $ch, $options );
        $content = curl_exec( $ch );
        curl_close( $ch );

        return $content;
}

echo get_web_page("http://en.wikipedia.org/wiki/ó");
相关问题