Jump to content
rayj00

New At PHP....

Recommended Posts

You cannot change allow_url_fopen using ini_set.
from php.net..
Name Default Changeable Changelogallow_url_fopen "1" PHP_INI_ALL PHP_INI_ALL in PHP <= 4.3.4.
is not that mean that its changebale from including iniset? (PHP_INI_ALL)

Share this post


Link to post
Share on other sites

Look at the note for the option here:http://www.php.net/manual/en/filesystem.co...allow-url-fopen

Note: This setting can only be set in php.ini due to security reasons.
The appendix of configuration options does indicate that it is PHP_INI_ALL, but this is a security feature and it doesn't make sense that you would be able to change a security feature at runtime.Even so, a script like this would test it:
<?phperror_reporting(E_ALL);ini_set('display_errors', 1);$fname = 'http://www.google.com';echo 'setting option to on...<br>';ini_set('allow_url_fopen', 1);echo 'opening connection...<br>';$data = file_get_contents($fname);echo 'got ' . strlen($data) . ' bytes<br>';echo 'setting option to off...<br>';ini_set('allow_url_fopen', 0);echo 'opening connection...<br>';$data = file_get_contents($fname);echo 'got ' . strlen($data) . ' bytes<br>';?>

If you run that script, and changing the option works, then the expected output would be the echo statements you see, with the first one reporting that it got several thousand bytes, followed by a notification that the option is being turned off, then an error message to indicate it couldn't open the file (and possibly a notice from strlen because it's checking a boolean). If you see that, then you can change the option. If you see more error messages than that, then you can't.

Share this post


Link to post
Share on other sites
Look at the note for the option here:http://www.php.net/manual/en/filesystem.co...allow-url-fopenThe appendix of configuration options does indicate that it is PHP_INI_ALL, but this is a security feature and it doesn't make sense that you would be able to change a security feature at runtime.Even so, a script like this would test it:
<?phperror_reporting(E_ALL);ini_set('display_errors', 1);$fname = 'http://www.google.com';echo 'setting option to on...<br>';ini_set('allow_url_fopen', 1);echo 'opening connection...<br>';$data = file_get_contents($fname);echo 'got ' . strlen($data) . ' bytes<br>';echo 'setting option to off...<br>';ini_set('allow_url_fopen', 0);echo 'opening connection...<br>';$data = file_get_contents($fname);echo 'got ' . strlen($data) . ' bytes<br>';?>

If you run that script, and changing the option works, then the expected output would be the echo statements you see, with the first one reporting that it got several thousand bytes, followed by a notification that the option is being turned off, then an error message to indicate it couldn't open the file (and possibly a notice from strlen because it's checking a boolean). If you see that, then you can change the option. If you see more error messages than that, then you can't.

oohh...got it now..did not know that...thanks for clearing out!i think they should somehow mark it in apendix too.

Share this post


Link to post
Share on other sites

Ok, I'm not sure what the heck is going on, unless my provider fixed something.Anyway, it looks like things are working...maybe..As I don't have a lot of experience with XML, javascript or json, here is some of theXML output:−<ipb>−<categories>−<category> <id>24</id> <name>Main Political Forums</name>−<url>http://liberalforum.org/liberalforum/xml.php?showforum=24</url>−<forums>−<forum><id>44</id><name>No Holds Barred Political Forum</name>And here us the json output of the same XML:{"categories":{"category":[{"id":"24","name":{},"url":{},"forums":{"forum":[{"id":"44","name":{},Now in the json output, why aren't I seeing the data for <name>, <url>, <name>???Thanks,Ray

Share this post


Link to post
Share on other sites

They've probably realised your problem is you need allow_url_fopen on, so they've enabled it for you.As for why the name(s?) and URL(s?) are missing... I tried your example, i.e.

$xml = simplexml_load_file('http://liberalforum.org/liberalforum/xml.php?showtopic=97861');echo json_encode($xml);

And doing a "View Source" on the output shows all of the data, including all names and URLs... could be a bug in your PHP version.I have 5.3.4... what's yours? (you can check that out with phpinfo() as well).

Share this post


Link to post
Share on other sites
They've probably realised your problem is you need allow_url_fopen on, so they've enabled it for you.As for why the name(s?) and URL(s?) are missing... I tried your example, i.e.
$xml = simplexml_load_file('http://liberalforum.org/liberalforum/xml.php?showtopic=97861');echo json_encode($xml);

And doing a "View Source" on the output shows all of the data, including all names and URLs... could be a bug in your PHP version.I have 5.3.4... what's yours? (you can check that out with phpinfo() as well).

PHP v 5.2.12

Share this post


Link to post
Share on other sites

I've also been able to test it with 5.2.13 (I don't have an older one), and it works there too... I have Windows 7 though... it could be a Linux specific issue.If your host agrees, you might want to ask them for a newer PHP version... the latest PHP 5.3 ideally.If even that doesn't solve this... what's the problem with sending XML anyway? JavaScript can handle XML just as well as JSON... better in fact (since all browsers have native support for it).

Share this post


Link to post
Share on other sites

You're probably just looking at the output in the browser, you need to view the source of the page like you're looking at the HTML code.

Share this post


Link to post
Share on other sites
You're probably just looking at the output in the browser, you need to view the source of the page like you're looking at the HTML code.
yes, the output I posted is from the browser, however when I do a View Source, the json output is the same, except it's all on a single line.

Share this post


Link to post
Share on other sites

The JSON output is not correct, it's not even valid syntax.

{"categories":{"category":[{"id":"24","name":{},"url":{},"forums":{"forum":[{"id":"44","name":{},

That's an object with a property called categories which is another object. The category object has an array called category, but the array doesn't end. It starts with that first square bracket, and the first element in the array is an object which also doesn't end. It has id, name, url, and forums properties. The forums property is another object that doesn't end, which has a property called forum that is another array that also doesn't end. The comma at the end of the JSON string is a dead giveaway that there's something missing. If I get rid of that comma on the end and close everything so it's at least syntactically valid, it would look like this:

{  "categories":  {	"category":	[	  {		"id":"24",		"name":{},		"url":{},		"forums":		{		  "forum":		  [			{			  "id":"44",			  "name":{}			}		  ]		}	  }	]  }}

Share this post


Link to post
Share on other sites
The JSON output is not correct, it's not even valid syntax.
{"categories":{"category":[{"id":"24","name":{},"url":{},"forums":{"forum":[{"id":"44","name":{},

That's an object with a property called categories which is another object. The category object has an array called category, but the array doesn't end. It starts with that first square bracket, and the first element in the array is an object which also doesn't end. It has id, name, url, and forums properties. The forums property is another object that doesn't end, which has a property called forum that is another array that also doesn't end. The comma at the end of the JSON string is a dead giveaway that there's something missing. If I get rid of that comma on the end and close everything so it's at least syntactically valid, it would look like this:

{  "categories":  {	"category":	[	  {		"id":"24",		"name":{},		"url":{},		"forums":		{		  "forum":		  [			{			  "id":"44",			  "name":{}			}		  ]		}	  }	]  }}

This is not all of the json output.......I am only showing some of it to compare to the XML I also posted.

Share this post


Link to post
Share on other sites

here is all of the json output:{"categories":{"category":[{"id":"24","name":{},"url":{},"forums":{"forum":[{"id":"44","name":{},"url":{},"description":{},"type":"1","topics":"71,364","replies":"1,589,458","lastpost":{"date":"1294858424","name":{},"id":"98121","url":{},"user":{"id":"19257","name":{},"url":{}}}},{"id":"40","name":{},"url":{},"description":{},"type":"1","topics":"2,370","replies":"23,468","lastpost":{"date":"1294857954","name":{},"id":"98066","url":{},"user":{"id":"14821","name":{},"url":{}}}},{"id":"32","name":{},"url":{},"description":{},"type":"1","topics":"1,952","replies":"21,756","lastpost":{"date":"1294856613","name":{},"id":"97079","url":{},"user":{"id":"20182","name":{},"url":{}}}}]}},{"id":"26","name":{},"url":{},"forums":{"forum":[{"id":"27","name":{},"url":{},"description":{},"type":"1","topics":"199","replies":"1,026","lastpost":{"date":"1294853131","name":{},"id":"65249","url":{},"user":{"id":"20390","name":{},"url":{}}}},{"id":"43","name":{},"url":{},"description":{},"type":"1","topics":"82","replies":"1,555","lastpost":{"date":"1294743842","name":{},"id":"85419","url":{},"user":{"id":"20390","name":{},"url":{}}}}]}}]},"statistics":{"posts":"1,721,156","members":"11,496","user":{"id":"20579","name":"JPettingill1","url":"http:\/\/liberalforum.org\/liberalforum\/xml.php?http:\/\/liberalforum.org\/liberalforum\/xml.php?showuser=20579"},"onlinerecord":"800"},"activeusers":{"total":"67","guests":"45","anonymous":"0","users":{}},"events":{"0":"\n\t\t\t\n\t\t\t\t\n\t\t\t\n\t\t"},"birthdays":{"0":"\n\t\t\n\t\t"}}

Share this post


Link to post
Share on other sites

That looks much better. All of the name and url properties are empty objects. The numbers of topics and replies and the information about the last post are there though. But for whatever reason the file you're getting that information from is not populating the names, urls, or descriptions with any data.

Share this post


Link to post
Share on other sites

Wait, what's the URL of the XML you are testing?I tested the sample code I gave you (URL of the XML is http://liberalforum.org/liberalforum/xml.p...howtopic=97861), and it outputs a much larger JSON string, in which, like I said, the names and URLs are present.

Share this post


Link to post
Share on other sites

Everything is in the XML so I don't get it!??Here is the associated XML in it's entirety:-<ipb>−<categories>−<category><id>24</id><name>Main Political Forums</name>−<url>http://liberalforum.org/liberalforum/xml.php?showforum=24</url>−<forums>−<forum><id>44</id><name>No Holds Barred Political Forum</name>−<url>http://liberalforum.org/liberalforum/xml.php?showforum=44</url>−<description>This political chat room welcomes those who are strident in their beliefs. All are welcome. Ideas, opinions, etc. that some would consider "unpopular" should be discussed in this room. </description><type>1</type><topics>71,355</topics><replies>1,589,278</replies>−<lastpost><date>1294847632</date><name>Arronssong Proves He's ...</name><id>97869</id>−<url>http://liberalforum.org/liberalforum/xml.p...iew=getlastpost</url>−<user><id>14239</id><name>chickie</name>−<url>http://liberalforum.org/liberalforum/xml.php?showuser=14239</url></user></lastpost></forum>−<forum><id>40</id><name>"Liberals Only" Political Chat Room</name>−<url>http://liberalforum.org/liberalforum/xml.php?showforum=40</url>−<description>Political chat room about current news and not-so-current events. Liberals, including Progressives, Leftists, Greens, Democrats, etc. debate today's most pressing issues. Right wingers should not enter. </description><type>1</type><topics>2,370</topics><replies>23,465</replies>−<lastpost><date>1294830679</date><name>Half Million New Jobs Natio...</name><id>97771</id>−<url>http://liberalforum.org/liberalforum/xml.p...iew=getlastpost</url>−<user><id>20479</id><name>dballnyc</name>−<url>http://liberalforum.org/liberalforum/xml.php?showuser=20479</url></user></lastpost></forum>−<forum><id>32</id><name>The Water Cooler Chat Room</name>−<url>http://liberalforum.org/liberalforum/xml.php?showforum=32</url>−<description>Take a break from chatting politics in the Water Cooler. Chat about off-topic interests (non-political/policy). No Politics here!</description><type>1</type><topics>1,952</topics><replies>21,755</replies>−<lastpost><date>1294833279</date><name>Help With The Personal Mess...</name><id>98010</id>−<url>http://liberalforum.org/liberalforum/xml.p...iew=getlastpost</url>−<user><id>19201</id><name>NeoConvict</name>−<url>http://liberalforum.org/liberalforum/xml.php?showuser=19201</url></user></lastpost></forum></forums></category>−<category><id>26</id><name>Perspectives On Helping America</name>−<url>http://liberalforum.org/liberalforum/xml.php?showforum=26</url>−<forums>−<forum><id>27</id><name>Political Books and Related Literature Forum</name>−<url>http://liberalforum.org/liberalforum/xml.php?showforum=27</url>−<description>Read any good political books lately? All are welcome to join in on the chat. The forum is limited to political journals, essays, books, and authors.</description><type>1</type><topics>199</topics><replies>1,025</replies>−<lastpost><date>1294798379</date><name>The Communist Manifesto</name><id>65249</id>−<url>http://liberalforum.org/liberalforum/xml.p...iew=getlastpost</url>−<user><id>20572</id><name>WorkingClassNow</name>−<url>http://liberalforum.org/liberalforum/xml.php?showuser=20572</url></user></lastpost></forum>−<forum><id>43</id><name>Health Insurance For All Americans</name>−<url>http://liberalforum.org/liberalforum/xml.php?showforum=43</url>−<description>Our Forum has taken on the mission of insuring every American.<br />Insurance agents will be supplied free leads to help us accomplish this goal.</description><type>1</type><topics>82</topics><replies>1,555</replies>−<lastpost><date>1294743842</date><name>Green Healthcare Is Possibl!</name><id>85419</id>−<url>http://liberalforum.org/liberalforum/xml.p...iew=getlastpost</url>−<user><id>20390</id><name>TheChampII</name>−<url>http://liberalforum.org/liberalforum/xml.php?showuser=20390</url></user></lastpost></forum></forums></category></categories>−<statistics><posts>1,720,962</posts><members>11,496</members>−<user><id>20579</id><name>JPettingill1</name>−<url>http://liberalforum.org/liberalforum/xml.p...?showuser=20579</url></user><onlinerecord>800</onlinerecord></statistics>−<activeusers><total>50</total><guests>34</guests><anonymous>0</anonymous>−<users><a href='http://liberalforum.org/liberalforum/xml.php?showuser=17336' title='A minute ago'>BeachBuff</a>,<a href='http://liberalforum.org/liberalforum/xml.php?showuser=14712' title='A minute ago'>aaronssongs</a>,<a href='http://liberalforum.org/liberalforum/xml.php?showuser=18954' title='A minute ago'>lucifershammer</a>,<a href='http://liberalforum.org/liberalforum/xml.php?showuser=14239' title='A minute ago'>chickie</a>,<a href='http://liberalforum.org/liberalforum/xml.php?showuser=18125' title='A minute ago'>RSIG</a>,<a href='http://liberalforum.org/liberalforum/xml.php?showuser=17001' title='A minute ago'>neue regel</a>,<a href='http://liberalforum.org/liberalforum/xml.php?showuser=16148' title='A minute ago'>blackdog</a>,<a href='http://liberalforum.org/liberalforum/xml.php?showuser=11768' title='A minute ago'>Slowmotion426</a>,<a href='http://liberalforum.org/liberalforum/xml.php?showuser=19926' title='2 minutes ago'>leftjohn</a>,<a href='http://liberalforum.org/liberalforum/xml.php?showuser=13032' title='4 minutes ago'>plectocomia</a>,<a href='http://liberalforum.org/liberalforum/xml.php?showuser=11520' title='4 minutes ago'>RichClem</a>,<a href='http://liberalforum.org/liberalforum/xml.php?showuser=18300' title='7 minutes ago'>AdamSmith101</a>,<a href='http://liberalforum.org/liberalforum/xml.php?showuser=16672' title='7 minutes ago'>byebyecons</a>,<a href='http://liberalforum.org/liberalforum/xml.php?showuser=18636' title='8 minutes ago'>diesel79</a>,<a href='http://liberalforum.org/liberalforum/xml.php?showuser=18824' title='11 minutes ago'>Uri Nation</a>,<a href='http://liberalforum.org/liberalforum/xml.php?showuser=19406' title='12 minutes ago'>Annoyed Liberall</a></users></activeusers><events> </events><birthdays> </birthdays></ipb>

Share this post


Link to post
Share on other sites

Nailed it! (Jon Stewart style)json_encode() appears to have a problem serializing CDATA nodes... perhaps because those nodes are not exactly exposed or something...A workaround is to explicitly eliminate the CDATA nodes upon loading, and only then serialize the SimpleXML object... like so:

$url = 'http://liberalforum.org/liberalforum/xml.php?showtopic';$xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);echo json_encode($xml);

The LIBXML_NOCDATA is the key part here.

Share this post


Link to post
Share on other sites
Nailed it! (Jon Stewart style)json_encode() appears to have a problem serializing CDATA nodes... perhaps because those nodes are not exactly exposed or something...A workaround is to explicitly eliminate the CDATA nodes upon loading, and only then serialize the SimpleXML object... like so:
$url = 'http://liberalforum.org/liberalforum/xml.php?showtopic';$xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);echo json_encode($xml);

The LIBXML_NOCDATA is the key part here.

I can't test this right now but if it does the trick for me.....AWESOME and YOU DA MAN!!!I'll check back with you later and let you know!!!Thanks for taking time to look at this! I really appreciate it!Ray

Share this post


Link to post
Share on other sites
Nailed it! (Jon Stewart style)json_encode() appears to have a problem serializing CDATA nodes... perhaps because those nodes are not exactly exposed or something...A workaround is to explicitly eliminate the CDATA nodes upon loading, and only then serialize the SimpleXML object... like so:
$url = 'http://liberalforum.org/liberalforum/xml.php?showtopic';$xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);echo json_encode($xml);

The LIBXML_NOCDATA is the key part here.

Ok I tried it and it sure looks a lot better!One thing though, I am seeing double the output. If you start at the bottom and work back you'll start seeingthe empty tags (<url>, <description>, etc.Ray

Share this post


Link to post
Share on other sites

Errr... It seems to me you're missing on the definiton of CDATA.CDATA is a syntax sugar for more easily writing out text that contains a lot of "<", ">" and "&".For example

<description><![CDATA[<div>TEST</div>]]></description>

is semantically equivalent to

<description><div>TEST</div></description>

The LIBXML_NOCDATA option turns all CDATAs into actual text nodes, i.e. it treats the first code as if it's the second. When you turn this to JSON, XML entities are written out literally, similarly to how they look in a CDATA, so the above example would turn into

"description":"<div>TEST</div>"

Or am I missing something... example?

Share this post


Link to post
Share on other sites
Errr... It seems to me you're missing on the definiton of CDATA.CDATA is a syntax sugar for more easily writing out text that contains a lot of "<", ">" and "&".For example
<description><![CDATA[<div>TEST</div>]]></description>

is semantically equivalent to

<description><div>TEST</div></description>

The LIBXML_NOCDATA option turns all CDATAs into actual text nodes, i.e. it treats the first code as if it's the second. When you turn this to JSON, XML entities are written out literally, similarly to how they look in a CDATA, so the above example would turn into

"description":"<div>TEST</div>"

Or am I missing something... example?

Go here: http://www.novir.us/test.php?url=http://li...l.php?showtopicThe top stuff returned in the browser looks good.At the bottom of the browser, there looks to be about 12 lines with blue text. About the 3rd blue line you can find:{"categories":{"category":[{"id":"24","name":{},"url":{},"forums":{"forum":[{"id":"44","name":{},"url":{},"description":{},"type":"1","topics":"71,384","replies":"1,589,844","lastpost": etc....Notice noting in the name , url or forum. Should I just ignore these?Ray

Share this post


Link to post
Share on other sites

By default, anything you output in PHP is assumed to be HTML. Since HTML is very forgiving, it tried to parse any HTML regardless of context. In this case, the "<a" in the JSON string creates a link.Add this before echoing anything:

header('Cotent-Type: application/json');

and you'll instead make the browser treat the content as JSON instead of HTML. This will most likely make the browser display a download box instead of displaying anything, but if you save and open the generated file, you'll see everything is right. And don't worry about JavaScript reading this text when accessing the PHP file - it will read it just fine... with or without this addition.

Share this post


Link to post
Share on other sites
By default, anything you output in PHP is assumed to be HTML. Since HTML is very forgiving, it tried to parse any HTML regardless of context. In this case, the "<a" in the JSON string creates a link.Add this before echoing anything:
header('Cotent-Type: application/json');

and you'll instead make the browser treat the content as JSON instead of HTML. This will most likely make the browser display a download box instead of displaying anything, but if you save and open the generated file, you'll see everything is right. And don't worry about JavaScript reading this text when accessing the PHP file - it will read it just fine... with or without this addition.

Hmmmm....I don't really want to see a download box. The json output will be echo'd back to anotherapplication that will parse out the needed info.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...