1

i have a php file that return json. here my php file.

http://dev.mediaodd.com/data/testjson.php

if i use writing testjson.json file it will success but if i use .php it will failed.

this is my output script:

<?php
// copy file content into a string var

$jsondata = file_get_contents('http://dev.mediaodd.com/data/testjson.php');
$obj = json_decode($jsondata,true);


echo $obj['company'][0]['id'];

?>

what did i do wrong?

file_get_contents cant read .php file?

my testjson.php file return same as testjason.json file.

1 Answers1

0

after clarification by OP, the following is not the case

file_get_contents cannot read from url resource unless this has been setup explicitly in php.ini configuration (not recommended mostly for security reasons).

So you should not use file_get_contents('http://dev.mediaodd.com/data/testjson.php'); to read an http resource, but instead read from local php files only that exist in the (server) disk

Community
  • 1
  • 1
Nikos M.
  • 8,033
  • 4
  • 36
  • 43
  • i already allow it before i post this question. so the problem i have is not what u said. can i use 'code'file_get_contents('http://dev.mediaodd.com/data/testjson.php?id=abc'); that have get method? – Shaiful Ezani May 26 '15 at 12:11
  • It's the other way round, url wrappers are ON by default and you would be explicitly turn them off, answer is not correct. – Daniel W. May 26 '15 at 12:17
  • @DanFromGermany for most default php configurations used in production, the functionality is OFF – Nikos M. May 26 '15 at 13:40
  • The last 30 webservers I was on, which I was not admin of, had it ON. The default also is ON. Wrappers are enabled by default, too. I cannot confirm "most" servers have it disabled... – Daniel W. May 26 '15 at 13:42
  • @DanFromGermany, hmm ok, but you will agree that it is not recommended to leave them ON mainly for security reasons and there are lot of references on the interweb about specificaly php configuration and security mentioning this – Nikos M. May 26 '15 at 13:47
  • @NikosM. I think you do mistake the configuration for fopen/file_get_contents with `allow_url_include`. That one is disabled by default and presents a big security risk. The other one, `allow_url_fopen` is on by default, it allows you to read feeds and json for example. See http://php.net/manual/en/filesystem.configuration.php – Daniel W. May 26 '15 at 13:58