David
David

Reputation: 11

I can't receive json data from a yii2-advanced app to another yii2-advanced-app with file_get_contents or curl

I have a Yii2 API app, and it is working correctly, when I trying to reach some data from a simple php code, using get_file_contents function or using curl. But if I want to achieve the same result with the same functions from another Yii2 app, the data will be arriving with an additional hidden character on the front of the data like '\ufeff'{data}. That happens in all time when I using a Yii2 app. This is generating some problems of using the data.

Example:
Yii2 API app: http://example.com/api/v1/user-data
Yii2 app: 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"}

When I trying to print out the data with echo the result is looking good but, using utf8_encode function shows the hidden chars what are making the problem on json_decode.

If I set the response format to application/json, will be appear the next error message: "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data". Because  or \ufeff added automatically to the response data.

The chars appears when the data arrived through curl or file_get_contents.

I can handle this on my Yii2 app, I can remove that chars, and show the data correctly, but it isn't a great solution. I would like to know why happens that and how can I solve this problem. Any Idea?

Upvotes: 1

Views: 237

Answers (1)

MaartenDev
MaartenDev

Reputation: 5802

When returning gzipped data you need to process the result. This can be done manually:

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

$jsonData = json_decode($decoded, true);

checkout the docs.

or automatically using 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);

I would suggest looking into HTTP libraries such as guzzle(http://docs.guzzlephp.org/en/stable/) that handle this automatically a provide a clean way to make requests.

Upvotes: 0

Related Questions