Jump to content

New At PHP....


rayj00

Recommended Posts

Ok, so besides trying some PHP tutorials I thought I'd just post this here.I want to access a website to retrieve data in XML format and convert the XML into JSON.Then send it back (in JSON format) to the original application.I was given this PHP code to accomplish this:<?PHP $xml = simplexml_load_file(urldecode($_GET['url'])); echo json_encode($xml);?>I have a website where I can host this code. My question is is the above codeall I need (no html code)? Second, how would I call this code - http://somefile.php="url of XML" ?Thanks and I appreciate your comments.Ray

Link to comment
Share on other sites

  • Replies 86
  • Created
  • Last Reply
It sounds like the XML file is not loading. Add this to the top of the file:error_reporting(E_ALL);ini_set('html_errors', 1);ini_set('display_errors', 1);
When I looked at the return page source before all I seen was "false".Now, after adding what you suggested, I am seeing:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><META content="text/html; charset=windows-1252" http-equiv=Content-Type></HEAD><BODY></BODY></HTML>More ideas?
Link to comment
Share on other sites

Here's what I have now:<?PHPerror_reporting(E_ALL);ini_set('html_errors', 1);ini_set('display_errors', 1); $xml = simplexml_load_file(urldecode($_GET['url']));$txt="Hello World";echo $txt; echo json_encode($xml);?>I do not see the echo $txt in the browser or the browser View/source....

Link to comment
Share on other sites

Are you sure your server supports PHP? What happens if you run this file:<?php phpinfo(); ?>
I get PHP info like:PHP Version 5.2.12 System Linux boscgi1904.eigbox.net 2.6.35.8-nx #1 SMP Wed Nov 3 01:47:21 EDT 2010 i686 Build Date Feb 12 2010 18:19:15 Configure Command './configure' '--prefix=/usr/local/lib/php-5.2.12' '--disable-ipv6' '--without-apache' '--with-mysql=/usr' '--with-mysqli' '--with-gd' '--with-jpeg-dir=/usr' '--with-png-dir=/usr' '--with-tiff-dir=/usr' '--disable-debug' '--enable-discard-path' '--enable-inline-optimization' '--enable-mbstring' '--enable-mbstr-enc-trans' '--enable-memory-limit' '--with-regex=system' '--with-zlib' '--enable-ftp' '--enable-sockets' '--enable-magic-quotes' '--with-gettext' '--enable-wddx' '--with-gdbm' '--with-db4' '--with-freetype-dir=/usr' '--with-curl=/usr' '--with-mhash' '--with-xsl' '--enable-calendar' '--with-mcve' '--with-dom' '--with-iconv' '--with-xmlrpc' '--with-mcrypt' '--with-bzip2' '--with-ming=/usr' '--with-pspell' '--with-openssl' '--with-ttf' '--with-freetype-dir=/usr' '--enable-gd-native-ttf' '--with-imap' '--with-kerberos' '--with-imap-ssl' '--enable-bcmath' '--enable-dbase' '--enable-exif' '--with-pdo-mysql' Server API CGI Virtual Directory Support disabled Configuration File (php.ini) Path /usr/local/lib/php-5.2.12/lib Loaded Configuration File /usr/local/lib/php-5.2.12/lib/php.ini Scan this dir for additional .ini files (none) additional .ini files parsed (none) PHP API 20041225 PHP Extension 20060613 Zend Extension 220060519 Debug Build no Thread Safety disabled Zend Memory Manager enabled IPv6 Support disabled Registered PHP Streams https, ftps, compress.zlib, php, file, data, http, ftp Registered Stream Socket Transports tcp, udp, unix, udg, ssl, sslv3, sslv2, tls Registered Stream Filters zlib.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed
Link to comment
Share on other sites

I've changed the script as such:<?PHP$txt="Hello World";echo $txt;error_reporting(E_ALL);ini_set('html_errors', 1);ini_set('display_errors', 1); $xml = simplexml_load_file(urldecode($_GET['url'])); echo json_encode($xml);?>Now I see the Hello World and errors after that:Warning: simplexml_load_file() [function.simplexml-load-file]: URL file-access is disabled in the server configuration in /hermes/bosweb/web172/b1723/sl.novirusa/public_html/test.php on line 7Warning: simplexml_load_file(http://liberalforum.org/liberalforum/xml.php?showtopic=97861) [function.simplexml-load-file]: failed to open stream: no suitable wrapper could be found in /hermes/bosweb/web172/b1723/sl.novirusa/public_html/test.php on line 7Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://liberalforum.org/liberalforum/xml.php?showtopic=97861" in /hermes/bosweb/web172/b1723/sl.novirusa/public_html/test.php on line 7false

Link to comment
Share on other sites

It's technically not valid to have special characters like ":", "/", "?", "=" etc in a querystring, those characters should be replaced with escape sequences. You can get a list of character codes at asciitable.com. A space character has a hex code of 20, so a space in a URL should be replaced with "%20". If a character has a code of "3C", you would use "%3C" in the URL to represent that character.

Link to comment
Share on other sites

It's technically not valid to have special characters like ":", "/", "?", "=" etc in a querystring, those characters should be replaced with escape sequences. You can get a list of character codes at asciitable.com. A space character has a hex code of 20, so a space in a URL should be replaced with "%20". If a character has a code of "3C", you would use "%3C" in the URL to represent that character.
Changed the query to: http://www.novir.us/test.php?url=http%3A%2...owtopic%3D97861I'm getting the exact same errors....Warning: simplexml_load_file() [function.simplexml-load-file]: URL file-access is disabled in the server configuration in /hermes/bosweb/web172/b1723/sl.novirusa/public_html/test.php on line 7Warning: simplexml_load_file(http://liberalforum.org/liberalforum/xml.php?showtopic=97861) [function.simplexml-load-file]: failed to open stream: no suitable wrapper could be found in /hermes/bosweb/web172/b1723/sl.novirusa/public_html/test.php on line 7Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://liberalforum.org/liberalforum/xml.php?showtopic=97861" in /hermes/bosweb/web172/b1723/sl.novirusa/public_html/test.php on line 7false
Link to comment
Share on other sites

Right, the error means the server is not allowed to load a file over HTTP. Since your filename starts with http://, it will not load on that server without changing the server settings to allow it to open remote files. That's what this means:URL file-access is disabled in the server configuration

Link to comment
Share on other sites

Right, the error means the server is not allowed to load a file over HTTP. Since your filename starts with http://, it will not load on that server without changing the server settings to allow it to open remote files. That's what this means:URL file-access is disabled in the server configuration
My service provider is saying I have a script problem and that I should fix my script!I am sending them a burning response.In the mean time I am wondering if you could kindly test my script so I can have ammoto tell my provider that it should work.I would be VERY appreciative....Thanks,Ray
Link to comment
Share on other sites

I guarantee the error message means what I'm telling you. If you want to give a sample script, just give this:

<?phperror_reporting(E_ALL);ini_set('html_errors', 1);ini_set('display_errors', 1);$fname = 'http://liberalforum.org/liberalforum/xml.php?showtopic=97861';$contents = file_get_contents($fname);echo $contents;?>

That's a simple script, and will work if the URL-wrapper for opening files is enabled. The file_get_contents function is affected by the same limitations as your code, so that script will show the same error. Give them that script, show them the error message, and explain that you would like the URL-wrapper enabled for opening files. They may not choose to do it, but that's what you need to ask. This is the PHP setting in question:http://www.php.net/manual/en/filesystem.co...allow-url-fopenThe note there indicates that the option can only be set in php.ini, so they need to change php.ini if they're going to let you use that script on their server. Note the code above uses ini_set to change various options, but allow_url_fopen does not work with ini_set.

Link to comment
Share on other sites

I guarantee the error message means what I'm telling you. If you want to give a sample script, just give this:
<?phperror_reporting(E_ALL);ini_set('html_errors', 1);ini_set('display_errors', 1);$fname = 'http://liberalforum.org/liberalforum/xml.php?showtopic=97861';$contents = file_get_contents($fname);echo $contents;?>

That's a simple script, and will work if the URL-wrapper for opening files is enabled. The file_get_contents function is affected by the same limitations as your code, so that script will show the same error. Give them that script, show them the error message, and explain that you would like the URL-wrapper enabled for opening files. They may not choose to do it, but that's what you need to ask. This is the PHP setting in question:http://www.php.net/manual/en/filesystem.co...allow-url-fopenThe note there indicates that the option can only be set in php.ini, so they need to change php.ini if they're going to let you use that script on their server. Note the code above uses ini_set to change various options, but allow_url_fopen does not work with ini_set.

Great answer!Is that setting GLOBAL? I imagine they won't do it if it is....
Link to comment
Share on other sites

There are various ways to change PHP settings across server accounts, if they have the server set up to do that.
Here are the settings for File Open section of php.ini;;;;;;;;;;;;;;;;;;; Fopen wrappers ;;;;;;;;;;;;;;;;;;;; Whether to allow the treatment of URLs (like http:// or ftp://) as files.allow_url_fopen = On; Define the anonymous ftp password (your email address);from="john@doe.com"; Define the User-Agent string; user_agent="PHP"; Default timeout for socket based streams (seconds)default_socket_timeout = 60; If your scripts have to deal with files from Macintosh systems,; or you are running on a Mac and need to deal with files from; unix or win32 systems, setting this flag will cause PHP to; automatically detect the EOL character in those files so that; fgets() and file() will work regardless of the source of the file.; auto_detect_line_endings = OffUsing your last script you provided, i am now getting these Warnings:Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in /hermes/bosweb/web172/b1723/sl.novirusa/public_html/filewrap.php on line 8Warning: file_get_contents(http://liberalforum.org/liberalforum/xml.php?showtopic=97861) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in /hermes/bosweb/web172/b1723/sl.novirusa/public_html/filewrap.php on line 8Grrrrrr....I really appreciate you taking the time to help me on this.....Thanks,Ray
Link to comment
Share on other sites

I presume the most recent version of PHP is installed on the server of your website, (Apache, Red Hat...).As for your PHP question, you might find helpful information here (or at the very least, steer you in the right direction) PHP _GETAlso, you questioned whether or not HTML code is required. I'm not exactly sure what you mean by this...Good luck,

Link to comment
Share on other sites

You don't need to just create a PHP.ini file and place is somewhere. PHP needs to actually read this file, and whether or not that is happening can only be determined by your host.If you want to write to your host in a way that would not omit anything of the context, send them a link to this topic in additon to all of your comments.

Link to comment
Share on other sites

If you want to find out which file PHP is actually using, go back to that phpinfo page. It will list something like "Loaded Configuration File" which will point to the php.ini that it's actually using. That's the one that needs to be edited.

Link to comment
Share on other sites

if your hosting support diretory based php.ini then you will be able to load php.ini from your every directory..where you have to put your php.in (edited)i.oryou may add this at the top of your script...

ini_set("allow_url_fopen",1);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...