【问题标题】:I can't receive json data from a yii2-advanced app to another yii2-advanced-app with file_get_contents or curl我无法使用 file_get_contents 或 curl 从 yii2-advanced 应用程序接收 json 数据到另一个 yii2-advanced-app
【发布时间】:2020-01-04 20:47:34
【问题描述】:

我有一个 Yii2 API 应用程序,当我尝试使用 get_file_contents 函数或 curl 从简单的 php 代码获取一些数据时,它工作正常。但是,如果我想用另一个 Yii2 应用程序的相同功能实现相同的结果,则数据将在数据前面带有一个额外的隐藏字符,如 '\ufeff'{data}。当我使用 Yii2 应用程序时,这种情况总是会发生。这会产生一些使用数据的问题。

示例:
Yii2 API 应用程序:http://example.com/api/v1/user-data
Yii2 应用:http://example2.com/site/get-data-from-api

// Yii2 API app controller

public function behaviors()
{
    return [
    // response format
    [
       'class' => 'yii\filters\ContentNegotiator',
       'formats' => [
           'application/json' => Response::FORMAT_JSON,
       ],
    ],
    ],
}

// provide the username
public function actionUserData() 
{
    $model = UserData::findOne(['user_id' => 1]);
    return [
        'username' => $model->username,
    ];
}
// Yii2 app controller, getting some data from the Yii2 API app

public function actionGetSomeDataFromApi() 
{
   //Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
   $data = file_get_contents('http://example.com/api/v1/user-data');

   echo $data; // return {"username" : "name"}
   echo utf8_encode($data); // return {"username" : "name"}

   $jsonData = json_decode($data, true);
   var_dump($jsonData); // will return NULL
}
// Yii2 api app response
HTTP/1.1 200 OK
Date: Sun, 01 Sep 2019 14:36:27 GMT
Server: Apache/2.4.18 (Ubuntu)
Set-Cookie: yii2-api-app=r8ftl2c2la763c8lq0sevubfj1; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Content-Length: 18
Content-Type: application/json; charset=UTF-8

{"username":"name"}

当我尝试使用 echo 打印数据时,结果看起来不错,但是使用 utf8_encode 函数显示隐藏的字符是什么导致 json_decode 出现问题。

如果我将响应格式设置为 application/json,将出现下一条错误消息:“SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data”。因为  或 \ufeff 会自动添加到响应数据中。

当数据通过 curl 或 file_get_contents 到达时出现字符。

我可以在我的 Yii2 应用程序上处理这个问题,我可以删除这些字符,并正确显示数据,但这不是一个很好的解决方案。我想知道为什么会发生这种情况以及如何解决这个问题。有什么想法吗?

【问题讨论】:

  • 会使用trim($data) 解决您的问题吗?
  • 是的,我可以修剪数据或者使用str_replace,preg_replace,它可以解决问题,但问题是为什么会这样?为什么通过file_get_contents返回带有奇怪字符的数据,只有当数据来自Yii2应用程序到另一个Yii2应用程序时才卷曲。
  • 其他 api 是否使用 gzip 等任何形式的压缩?
  • 接受编码:gzip,放气

标签: php json curl yii2 yii2-advanced-app


【解决方案1】:

返回 gzip 压缩后的数据时,您需要处理结果。这可以手动完成:

$data = file_get_contents('http://example.com/api/v1/user-data');
$decoded = gzinflate(substr($data,10));

$jsonData = json_decode($decoded, true);

查看docs

或自动使用 curl

$url = 'http://example.com/api/v1/user-data';
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_ENCODING, 1);
$output = curl_exec($ch); 

curl_close($ch); 
$jsonData = json_decode($output, true);

我建议研究自动处理此问题的 HTTP 库,例如 guzzle(http://docs.guzzlephp.org/en/stable/),以提供一种干净的方式来发出请求。

【讨论】:

  • 感谢您的回答,它很有用,但不幸的是问题仍然存在,结果是一样的。:(
  • 您能否将您的 api(带有标题)的整个响应添加到您的帖子中? @大卫
  • 当然,添加了 api 的响应。
  • 您可以尝试从actionUserData 返回一个空数组,例如return []。还有问题吗? @大卫
  • 当然,但结果是一样的。
猜你喜欢
  • 2016-06-04
  • 2017-08-19
  • 1970-01-01
  • 2015-11-10
  • 2015-03-31
  • 2015-08-06
  • 2015-10-31
  • 1970-01-01
相关资源
最近更新 更多