Embed PHP in XML

We have used an .xml file for a number of years to generate a dynamic feed. To accomplish this we have embedded php in the xml file. (note we are not processing a php file to generate an xml file) This has worked well, but it just stopped working. The embedded php is not being processed but is being parsed as xml (well, malformed xml).

I thought that maybe someone edited our httpd.conf file and removed the "AddType application/x-httpd-php .xml" line that has apache preprocess the php, but the line is in the httpd.conf file.

The feed file (XXXX.xml) has not changed in quite a few years, so any idea why the php is not being processed?

Here is the first couple of lines of the xml file:

<?php
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
....PHP Code.....
?>

<rss version="2.0"

<?php
.....More PHP Code.....
echo ......xml stored in php strings........
?>
</rss>
RSchnabelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
From the code you posted it looks like you are using PHP to generate an XML - which is a bit different from how you phrased the question.

In otherwords your input file is being interpreted by the server as a PHP file and is being parsed by the PHP interpreter but the file contains a mixture of PHP statements and XML output.

What you need to do is

1. copy the file to XXXXX.php
2. Add the following to the file
<?php
error_reporting(E_ALL)

3. Call the file directly in your browser and see if there are any errors.

Alternatively post more of the file here so we can get an idea of what the problem is - you have provided a broad overview of the problem which is not enough for us to narrow down what the problem could be.
0
Ray PaseurCommented:
First test: Store this script, shown here in its entirety, in a file named phpinfo.xml and run it from your browser address bar.
<?php phpinfo();

Open in new window

If that works, the failure is not in the configuration.  If that does not produce the phpinfo() page, the server is now misconfigured and is not parsing XML documents through the PHP parser.

Check with your hosting company to see if they have upgraded PHP recently.  Default character set changes at PHP 5.4, and that may matter.  Please post a complete copy of the PHP script, including all of the embedded XML, and a link to the script on your server, so we can see the script and see its behavior at the same time.  I think the use of error_reporting(E_ALL) is a very good idea.  You do not want to suppress error messages; you want to visualize them and fix the underlying error condition.
0
RSchnabelAuthor Commented:
Thank you both for the suggestions. When I renamed phpinfo.php to phpinfo.xml it worked fine. After displaying the error I found that it is not liking this line:

echo "<?xml version='1.0' encoding='UTF-8'?>\n";

and what it does not like is the line feed \n, When I remove the \n it spits an error a few lines down in the code where a String is being defined on the quote mark

$GetTextCodeQuery="Select ......
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Julian HansenCommented:
nothing wrong with that but try

echo "<?xml version='1.0' encoding='UTF-8'?>" . PHP_EOL;

See if that has a problem.
0
Julian HansenCommented:
However, I would look at the rest of the code - there should be no issue with the \n

Can you post the first 20 lines or so of your code?
0
RSchnabelAuthor Commented:
When I changed it to echo "<?xml version='1.0' encoding='UTF-8'?>" . PHP_EOL; it got past that line but gave the parsing error on the $GetTextCodeQuery line pointing at the S in Select, see code below. I have included the first twenty lines of code as requested.

<?php
echo "<?xml version='1.0' encoding='UTF-8'?>".PHP_EOL;;
function GetTextCode ($l_Product, $l_Code) {
            global $pgPDO;
        $GetTextCodeQuery="Select prodtx_text from t_prod_text where prodtx_comp_code='001' and prodtx_store_code='CCI' and prodtx_lang_code='ENG' and prodtx_prod_code='".$l_Product."' and prodtx_textt_code='".$l_Code."';";
        foreach ($pgPDO->query($GetTextCodeQuery) as $row) {
                $ReturnValue=htmlspecialchars(trim($row['prodtx_text']), ENT_QUOTES);
                //echo "Previously Added Log File";
        }
        return $ReturnValue;
}

function GetMediaCode ($l_Product, $l_Code) {
            global $pgPDO;
        $GetTextCodeQuery="Select prdmobj_mmobj_ref from t_prod_mm_obj where prdmobj_comp_code='001' and prdmobj_store_code='CCI' and prdmobj_prod_code='".$l_Product."' and prdmobj_mmobjt_code='".$l_Code."';";
        foreach ($pgPDO->query($GetTextCodeQuery) as $row) {
                $ReturnValue=trim($row['prdmobj_mmobj_ref']);
                //echo "Previously Added Log File";
        }
        return $ReturnValue;
}
0
hieloCommented:
try explicitly adding the content type:
<?php
header('Content-Type: application/xml');
echo "<?xml version='1.0' encoding='UTF-8'?>".PHP_EOL;
...

Open in new window

0
RSchnabelAuthor Commented:
Did so, but did not solve issue.
0
Ray PaseurCommented:
I have to confess that I have never seen anyone write code this way.  Can you isolate and run the functions, independent of the creation of the XML document?  When I run this code, I get correct output, as far as it goes.
http://www.laprbass.com/RAY_temp_rschnabel.php

<?php // RAY_temp_rschnabel.php
error_reporting(E_ALL);


echo "<?xml version='1.0' encoding='UTF-8'?>".PHP_EOL;
function GetTextCode ($l_Product, $l_Code) {
            global $pgPDO;
        $GetTextCodeQuery="Select prodtx_text from t_prod_text where prodtx_comp_code='001' and prodtx_store_code='CCI' and prodtx_lang_code='ENG' and prodtx_prod_code='".$l_Product."' and prodtx_textt_code='".$l_Code."';";
        foreach ($pgPDO->query($GetTextCodeQuery) as $row) {
                $ReturnValue=htmlspecialchars(trim($row['prodtx_text']), ENT_QUOTES);
                //echo "Previously Added Log File";
        }
        return $ReturnValue;
}
function GetMediaCode ($l_Product, $l_Code) {
            global $pgPDO;
        $GetTextCodeQuery="Select prdmobj_mmobj_ref from t_prod_mm_obj where prdmobj_comp_code='001' and prdmobj_store_code='CCI' and prdmobj_prod_code='".$l_Product."' and prdmobj_mmobjt_code='".$l_Code."';";
        foreach ($pgPDO->query($GetTextCodeQuery) as $row) {
                $ReturnValue=trim($row['prdmobj_mmobj_ref']);
                //echo "Previously Added Log File";
        }
        return $ReturnValue;
}

Open in new window

0
hieloCommented:
Have you tried the following setting:
short_open_tag = Off
http://www.php.net/manual/en/ini.core.php#ini.short-open-tag

OR

instead of double quotes around the statement:
echo "<?xml version='1.0' encoding='UTF-8'?>".PHP_EOL;

Open in new window


try apostrophes:
echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;

Open in new window

0
Ray PaseurCommented:
I wonder if we have been seeing a redacted script.  An error showing up on the character after a quotation mark almost certainly means that there is an unbalanced pair of quotation marks.  But that has not been shown to us.  So I'm back to this request:
Please post a complete copy of the PHP script, including all of the embedded XML, and a link to the script on your server, so we can see the script and see its behavior at the same time.
0
RSchnabelAuthor Commented:
I want to thank everyone for the input, I have not quite solved the issue yet, but I am close. The problem is not the file, but it appears to be a permissions issue. If I copy the file to the root of our web server it runs correctly, but in the directory it is in it does not. I have checked the privileges on the root and the sub directory it is in and they are the same, and once the file was copied to the root I checked the privileges on the two files and they appear to be the same. Can there be a restriction in a configuration file that would prevent php from running on some directories apart from os privileges?
0
Ray PaseurCommented:
Uhh, yes.  It would be quite likely to arise from that, because we cannot reproduce the failure in our systems where we have all of the PHP scripts in the WWW root.
0
Bernard S.CTOCommented:
PHP probably enforces "basic safety", which implies that php scan access only in directories under the one the php script is currently running (note that this may be different from the directory in which the current php included file is running)
0
Ray PaseurCommented:
@fibo: That may be true, but on all of my Linux builds, I can readily include() PHP scripts from outside and above the WWW root.  These files have no URL but can be accessed by the server file system.  I keep passwords, connection variables, and other "singletons" out there to add a small measure of security by making them unavailable to the browser.  It seems to me that if I screwed up the permissions to those directories, I could hatch a catastrophe!
0
RSchnabelAuthor Commented:
As a test I opened up the permissions on the lower directory and php still would not execute the scripts in those directories. The thing that confuses me is that everything has worked fine for 5+ years and with the directory and file permissions wide open I would have thought that it should have worked just fine, but no......
0
Slick812Commented:
just a note from me, since this was working for "quite a few years" and has recently stopped working, it may be difficult for any of us experts to see a problem, since this is (or was) correctly working code. There must be a Server Software change that caused this, I know from experience that some upgrades to PHP 5.3  ,  5.4  and  5.5  have caused MAJOR problems in previously working PHP coding.  Although I have never heard of this particular problem with the php echo, but there are so many changes in PHP operations in 5.4 and 5.5 that probaly no one know most of them.
I am inclined to agree wid hielo , in that, ,  this line -
<?xml version='1.0' encoding='UTF-8'?>
has what can be interpreted as short PHP indicators <? by a text parser
I have NO IDEA if this will be any better, but you may try and using alternative text character, as in
echo '<'.chr(63).'xml version="1.0" encoding="UTF-8"'.chr(63).'>';
although It may be some sort of change in the PHP parser for string indicators " and '
but I am just guessing.
That it works in one directory and not another, just has me with No Clue for that, , as meaning something to fix this.

OH just remembered something else, some PHP server Administrators add software to PHP for protection, security, speed, and or other reasons, that was NOT there before, and can cause problems on a few not often used methods or coding in PHP code, perhaps this is a clue for the directory thing? ?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RSchnabelAuthor Commented:
Issue resolved by system administrator
0
Ray PaseurCommented:
Wow, after a couple of weeks of radio silence... How was the issue resolved, please?

And I believe we are entitled to an explanation of the grade, according to the grading guidelines, which are linked here for your reference:
http://support.experts-exchange.com/customer/portal/articles/481419
0
RSchnabelAuthor Commented:
My apologizes to all, I had not read the grading guideline.  I gave a average grade because while, I thought the suggestions were very good, ultimately they did not solve the problem. After reading the grading guideline I would be happy to change the grade (although I might need help from an expert to tell me how to do that).

As to what was done, I cannot give all of the details (because I was not the one who did it), but php was reinstalled.
0
Ray PaseurCommented:
OK, thanks.  Glad it's pointed in the right direction now.  Regards, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.