【问题标题】:Php file_get_contents Special Charactersphp file_get_contents 特殊字符
【发布时间】:2016-10-11 13:44:44
【问题描述】:

我将使用 api 来获取玩家详细信息。有些名称带有特殊字符。

名字鲍西

网址:https/eu.api.battle.net/wow/character/Blackrock/Bausí?fields=statistics&locale=en_GB&apikey=xxx

如果我使用 file_get_contens() 没有响应。

没有特殊字符的名称可以完美运行。我已经使用了 rawurlencode() 和 urlencode()。两者都不起作用。我能做些什么? 我读过一些关于 urlencode() 和 rawurlencode() 依赖于服务器的内容。

谢谢你

【问题讨论】:

  • 很可能您需要对 unicode 字符进行编码。
  • 试试这个帖子的答案:stackoverflow.com/questions/2236668/…
  • 不工作我已经完成了 $charname = mb_convert_encoding("Bausí", "HTML-ENTITIES", "UTF-8");并将其插入到网址中。还是不行
  • eu.api.battle.net/wow/character/Blackrock/… 仍然没有得到任何内容。但名称在 url 中是正确的。还有什么问题
  • file_get_contents() 失败

标签: php api server special-characters file-get-contents


【解决方案1】:

我不知道怎么了。

                $charname = mb_convert_encoding("Bausí", "HTML-ENTITIES", "UTF-8");
                $realm = "Blackrock";
                $_SESSION["region"] = "eu";
                $opts = array('https' => array('header' => 'Accept-Charset: UTF-8, *;q=0'));
                $context = stream_context_create($opts);

                $char_params = array(
                    'fields' => "statistics",
                    'locale' => "en_GB",
                    'apikey' => "my_api_key"
                );

                $char_url = "https://".$_SESSION['region'].".api.battle.net/wow/character/".str_replace("'","",$realm)."/".$charname."?".http_build_query($char_params);

                if (!$json_char_o = file_get_contents($char_url, false, $context)) {
                      $error = error_get_last();
                      echo "HTTPs request failed. Error was: " . $error['message'];
                }else{
                      echo $json_char_decoded = json_decode($json_char_o);

                }

它给我一个错误:

HTTP 请求失败。错误是:file_get_contents(https://eu.api.battle.net/wow/character/Blackrock/Bausí?fields=statistics&locale=en_GB&apikey=my_api_key):打开流失败:HTTP 请求失败! HTTP/1.1 404 未找到

响应应该是一个 json 对象。

如果我手动打开链接,它会完美运行。怎么了? :/

【讨论】:

    猜你喜欢
    • 2015-10-21
    • 2012-05-10
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    相关资源
    最近更新 更多