卷曲问题:URL中的变音符号

时间:2014-02-12 09:58:22

标签: php curl

我在使用变音符号(例如“ü”)获取网址时遇到问题。

例如“http://www.ebay.de/bhp/kühlschrank”:

我的剧本:

function getUrlContent($url)
{
    //echo $url;
    $ch = curl_init();


    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 10 seconds
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_ENCODING ,"UTF-8");


    $response=curl_exec($ch);
    $i = curl_getinfo($ch);
    echo "<pre>";
    print_r($i);
    return $response;

}

$url="http://www.ebay.de/bhp/kühlschrank";
$response = getUrlContent($url);

它总是会产生404。

任何想法?

3 个答案:

答案 0 :(得分:2)

您需要utf8_decode() $ url参数并添加 cURL 参数。 FOLLOWLOCATION

<?php
function getUrlContent($url)
{
    //echo $url;
    $ch = curl_init();


    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 10 seconds
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); //<------- I added it here !
    curl_setopt($ch, CURLOPT_ENCODING ,"UTF-8");


    $response=curl_exec($ch);
    $i = curl_getinfo($ch);
    echo "<pre>";
    print_r($i);
    return $response;

}

$url="http://www.ebay.de/bhp/kühlschrank";
$response = getUrlContent(utf8_decode($url)); //<---- utf8 decode !!

<强> OUTPUT :

Array
(
    [url] => http://pages.ebay.com/messages/DE_page_not_responding.html?RlogId=t6awipp%60c%7Fs%3F%3Ctof2e34e*%3B4c3-144258a5d5d-0x1f0
    [content_type] => text/html;charset=UTF-8
    [http_code] => 200
    [header_size] => 1233
    [request_size] => 264
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 1
    [total_time] => 1.235
    [namelookup_time] => 0.266
    [connect_time] => 0.485
    [pretransfer_time] => 0.485
    [size_upload] => 0
    [size_download] => 4611
    [speed_download] => 3733
    [speed_upload] => 0
    [download_content_length] => 4611
    [upload_content_length] => 0
    [starttransfer_time] => 0.735
    [redirect_time] => 0.469
    [certinfo] => Array
        (
        )

    [primary_ip] => 66.135.205.14
    [primary_port] => 80
    [local_ip] => 192.168.1.9
    [local_port] => 61581
    [redirect_url] => 
)

答案 1 :(得分:0)

穷人的另一种解决方案是使用URL的简单US-ASCII版本为Curl提供信息:

http://www.ebay.de/bhp/k%C3%BChlschrank

我通过使用Firefox获取页面并从位置栏复制URL来获取我的,但您也可以从PHP执行此操作:

// Assuming UTF-8
$url="http://www.ebay.de/bhp/" . rawurlencode("kühlschrank");

答案 2 :(得分:0)

Curl不会为您编码URL,您必须这样做。也就是说,它必须是URL编码的。严格来说,你不应该只是解码UTF8,因为它不是一回事。您应该使用rawurlencode()

    <?php
function getUrlContent($url)
{
      //echo $url;
      $ch = curl_init();


      curl_setopt($ch, CURLOPT_URL,$url);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 10 seconds
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch, CURLOPT_HEADER, 1);
      curl_setopt($ch, CURLOPT_ENCODING ,"UTF-8");


      $response=curl_exec($ch);
      $i = curl_getinfo($ch);
      echo "<pre>";
      print_r($i);
      return $response;

}

$url="http://www.ebay.de/bhp/" . rawurlencode( "kühlschrank" );
$response = getUrlContent($url);
?>

<强>输出:

    Array
(
    [url] => http://www.ebay.de/bhp/k%C3%BChlschrank
    [content_type] => text/html;charset=utf-8
    [http_code] => 200
    [header_size] => 1007
    [request_size] => 94
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 1.669702
    [namelookup_time] => 0.606492
    [connect_time] => 0.744441
    [pretransfer_time] => 0.744556
    [size_upload] => 0
    [size_download] => 44032
    [speed_download] => 26371
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => 0
    [starttransfer_time] => 1.386599
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

    [primary_ip] => 23.2.16.17
    [primary_port] => 80
    [local_ip] => 10.1.1.2
    [local_port] => 56592
    [redirect_url] => 
)