0

I have an api in one of my sites. When I try to fetch contents width file_get_contents an error occures. I have tested the url in the browser and it's okay. Here is my url dilox.ir/category/مجله-ماشین/?json=1 and here is my php code

$cat = 'مجله-ماشین';
$json = file_get_contents('http://dilox.ir/category/' . $cat . '/json=1&count=10&page=' . $page);

after I execute my code. I face with this error

{ "name":"PHP Notice",
  "message":"Trying to get property of non-object",
  "code":8,
  "type":"yii\\base\\ErrorException",
  "file":"C:\\xampp\\htdocs\\pkhodro\\controllers\\ApiController.php",
  "line":142,
  "stack-trace":["#0 C:\\xampp\\htdocs\\pkhodro\\controllers\\ApiController.php(142): yii\\base\\ErrorHandler->handleError(8, 'Trying to get p...', 'C:\\\\xampp\\\\htdocs...', 142, Array)",
                "#1 [internal function]: app\\controllers\\ApiController->actionGet_magazines(1)",
                "#2 C:\\xampp\\htdocs\\pkhodro\\vendor\\yiisoft\\yii2\\base\\InlineAction.php(55): call_user_func_array(Array, Array)",
                "#3 C:\\xampp\\htdocs\\pkhodro\\vendor\\yiisoft\\yii2\\base\\Controller.php(154): yii\\base\\InlineAction->runWithParams(Array)",
                "#4 C:\\xampp\\htdocs\\pkhodro\\vendor\\yiisoft\\yii2\\base\\Module.php(454): yii\\base\\Controller->runAction('get_magazines', Array)",
                "#5 C:\\xampp\\htdocs\\pkhodro\\vendor\\yiisoft\\yii2\\web\\Application.php(87): yii\\base\\Module->runAction('api/get_magazin...', Array)",
                "#6 C:\\xampp\\htdocs\\pkhodro\\vendor\\yiisoft\\yii2\\base\\Application.php(375): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))",
                "#7 C:\\xampp\\htdocs\\pkhodro\\web\\index.php(12): yii\\base\\Application->run()","#8 {main}"
                ]
}

it shows that it can't read the url. Same code is executing successfully for a category without hyphen

Please see this Edit

I tested my code. file_get_contents return json for me, but json_decode can't decode the returned json and cause the error

Behzad Hassani
  • 2,129
  • 4
  • 30
  • 51
  • That is a PHP error, read it and fix the error – RiggsFolly Oct 17 '16 at 19:33
  • Yes , of course. But what error? @RiggsFolly As you see this link is shown in browser properly – Behzad Hassani Oct 17 '16 at 19:35
  • It tells you, you are accessing a property of a non-object, so something is not an object that should be. This would be a good place to look `ApiController.php", "line":142,` – ArtisticPhoenix Oct 17 '16 at 19:37
  • I see. but when I change my current category to a category without hyphen it work okay :( @ArtisticPhoenix – Behzad Hassani Oct 17 '16 at 19:39
  • I don't know Yii, but I assume these Categories are in a database? if so does the category exist. It's typical for some queries to return false when there are no results instead of an object. – ArtisticPhoenix Oct 17 '16 at 19:41
  • It's very strange for me, here is url which I call in the code `http://dilox.ir/category/مجله-ماشین/?json=1&count=10&page=1` and I can see this in browser but file_get_content can not read this :-| @ArtisticPhoenix – Behzad Hassani Oct 17 '16 at 19:44
  • please see the edit @ArtisticPhoenix – Behzad Hassani Oct 17 '16 at 19:50
  • Obviously that's not English, but I would say make sure the - is in the proper character set, I have very limited knowledge of foreign languages and how that affects the file system. – ArtisticPhoenix Oct 17 '16 at 20:48
  • http://stackoverflow.com/questions/7109143/what-characters-are-valid-in-a-url – RiggsFolly Oct 17 '16 at 23:28

0 Answers0