title: I can't receive json data from a yii2-advanced app to another yii2-advanced-app with file_get_contents or curl
body: <p>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.</p>
  
  <p>Example: <br>
  Yii2 API app: <a href="http://example.com/api/v1/user-data" rel="nofollow noreferrer">http://example.com/api/v1/user-data</a> <br>
  Yii2 app: <a href="http://example2.com/site/get-data-from-api" rel="nofollow noreferrer">http://example2.com/site/get-data-from-api</a></p>
  
  <pre class="lang-php prettyprint-override"><code>// Yii2 API app controller
  
  public function behaviors()
  {
      return [
      // response format
      [
         'class' =&gt; 'yii\filters\ContentNegotiator',
         'formats' =&gt; [
             'application/json' =&gt; Response::FORMAT_JSON,
         ],
      ],
      ],
  }
  
  // provide the username
  public function actionUserData() 
  {
      $model = UserData::findOne(['user_id' =&gt; 1]);
      return [
          'username' =&gt; $model-&gt;username,
      ];
  }
  </code></pre>
  
  <pre class="lang-php prettyprint-override"><code>// Yii2 app controller, getting some data from the Yii2 API app
  
  public function actionGetSomeDataFromApi() 
  {
     //Yii::$app-&gt;response-&gt;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
  }
  </code></pre>
  
  <pre><code>// 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"}
  </code></pre>
  
  <p>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. </p>
  
  <p>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. </p>
  
  <p>The chars appears when the data arrived through curl or file_get_contents.</p>
  
  <p>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?</p>