代码之家  ›  专栏  ›  技术社区  ›  Laurent

php-json_decode-发出解码字符串

  •  1
  • Laurent  · 技术社区  · 6 年前

    我正在使用deepl.com提供自动翻译的API。我通过curl调用API,得到一个JSON字符串作为返回,它看起来很好,但是由于某种原因不能被PHP解码。

    让我先演示一下我是如何发出卷曲的声音的:

    $content = "bonjour <caption>monsieur</caption> madame";
    $url = 'https://api.deepl.com/v2/translate';
    $fields = array(
            'text' => $content,
            'target_lang' => $lg,
            'tag_handling' => 'xml',
            'ignore_tags' => 'caption',
            'auth_key' => 'my_api_key');
    
    $fields_string = "";
    foreach($fields as $key=>$value) 
        { 
            $fields_string .= $key.'='.$value.'&'; 
        }
    
    rtrim($fields_string, '&');
    
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded','Content-Length: '. strlen($fields_string)));  
    
    $result = curl_exec($ch);
    
    curl_close($ch);
    

    如果我在这个阶段

    echo $result;
    

    我得到:

    {"translations":[{"detected_source_language":"FR","text":"Hola <caption>monsieur</caption> Señora"}]}
    

    我觉得没问题。如果我使用下面的代码-

    echo gettype($result);
    

    我得到“string”,它仍然正常,但现在,以下代码失败:

    $result = json_decode($result,true);
    print_r($result);
    

    输出为空!

    如果我现在这样做:

    $test = '{"translations":[{"detected_source_language":"FR","text":"Hola <caption>monsieur</caption> Señora"}]}';
    echo gettype($test);
    $test = json_decode($test,true);
    print_r($test);
    

    我得到了一个完美的阵列:

    (
    [translations] => Array
        (
            [0] => Array
                (
                    [detected_source_language] => FR
                    [text] => Hola <caption>monsieur</caption> Señora
                )
    
        )
    
    )
    

    除了将内容从api复制/粘贴到一个静态变量之外,我什么也没做,但是它是从api来的,而不是从api来的。

    你知道怎么回事吗?

    谢谢!

    劳伦特

    3 回复  |  直到 6 年前
        1
  •  1
  •   Stefan    6 年前

    确保设置 CURLOPT_RETURNTRANSFER 为真。只有那时才会 curl_exec 实际返回响应,否则将输出响应并返回布尔值,指示成功或失败。

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $result = curl_exec($ch);
    curl_close($ch);
    
    if ($result !== false) {
      $response = json_decode($result, true);
      // do something with $response
    } else {
      // handle curl error
    }
    
        2
  •  2
  •   Eilert Hjelmeseth    6 年前

    我以前也遇到过类似的问题,对我来说,问题在于从API返回的数据的编码是Unicode。我猜,当你复制/粘贴字符串时,你的硬代码最终会变成一种不同的编码,所以当传递到JSON解码时,它会很好地工作。

    php文档指定json_decode仅适用于utf-8编码字符串: http://php.net/manual/en/function.json-decode.php

    您可以使用mb_convert_encoding()将其转换为utf-8: http://php.net/manual/en/function.mb-convert-encoding.php

    在调用json_decode之前尝试此操作:

    $result = mb_convert_encoding($result, "UTF-8");
    
        3
  •  1
  •   SachaDee    6 年前

    如@eilert hjelmeseth所说,您的json字符串中有一些特殊字符=>“se_±ora”

    将字符串编码为utf8的另一种方法: utf8_encode() :

    $result = json_decode(utf8_encode($result),true);