【问题标题】:PHP CURL not working with UTF-8 CharacterPHP CURL 不适用于 UTF-8 字符
【发布时间】:2017-06-29 10:05:53
【问题描述】:

我尝试使用带有 PHP CURL 请求的 api 获取 Steam 项目,但问题是 CURL 不支持 UTF-8 作为请求。

我的 PHP 代码

$steam_key = $this->config->item("steam_key");
$name = "★ StatTrak™ Bayonet";
echo $url = "http://api.csgo.steamlytics.xyz/v1/prices/$name?key=$steam_key";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($ch);
echo $response;

我没有得到任何回应。

【问题讨论】:

  • 如果你把urlencode($name)作为参数呢?
  • 不是 UTF-8 的问题,是在 curl_init() 使用之前需要对 URL 进行编码。
  • 最好对值进行编码,而不是对完整的 url 进行编码(问号发生了什么事,在 url 中使用斜线符号?
  • 我使用了 urlencode 它给了我这个 url api.csgo.steamlytics.xyz/v1/prices/…
  • 还可以看到变量名也包含空格

标签: php curl utf-8 steam


【解决方案1】:

在与 cURL 一起使用之前对您的 URL 进行编码。

使用urlencode来完成这个任务:

$steam_key = $this->config->item("steam_key");
$name = urlencode("★ StatTrak™ Bayonet"); //Add function call here
echo $url = "http://api.csgo.steamlytics.xyz/v1/prices/$name?key=$steam_key";

【讨论】:

  • 我使用了 urlencode 它给了我这个 url api.csgo.steamlytics.xyz/v1/prices/…
  • 我在您的请求中没有看到您的?key=something。 @Sadikhasan
  • @Sadikhasan 查看您的网址。它不是有效的密钥。你有有效的钥匙吗?
  • 是的,我有有效的密钥,但它是安全的,所以我不能与你分享。这实际上不是键错误。
  • 然后使用这个键@Sadikhasan
猜你喜欢
  • 2012-06-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
  • 2017-11-15
  • 1970-01-01
相关资源
最近更新 更多