json_decode for Google Dictionary API

前端 未结 1 1798
我寻月下人不归
我寻月下人不归 2021-01-14 04:16

I am trying to decode the Google Dictionary API JSON response with PHP, but I am running into an issue where I cannot seem to parse the response (json_decode re

1条回答
  •  孤城傲影
    2021-01-14 04:54

    Google returns the result in the form of a function call. If you want to get rid of that you can do: So you have to strip that part off:

    $file = substr($file, 2, -10);
    

    Furthermore all the hex characters cause a mess. I don't know the best way of dealing with these (they should be converted to their character, but how to go about that easily requires thought). For testing purposes you can just strip them (to see that it then works). Try:

    $file = preg_replace("/\\\x[0-9a-f]{2}/", "", $file);
    

    So in the end you have:

    
    

    For converting the characters as needed, I suppose you could use preg_replace_callback, but I don't like that function, specially when the data comes from a remote source. Simpler may be to simply have a static mapping, eg:

    $from = array("\x3d", "\x22", ...);
    $to = array("=", "\"", ...);
    $file = str_replace($from, $to, $file);
    

    0 讨论(0)
提交回复
热议问题