-2

i'm looking how to Load the XML file in PHP and Return the data not redirect. Preferably to hide the xml content from the end user as much as i can.

I've seen something like this, but i can not make it work, please if you can write out the complete code with a link example..

     public function sendResponse($type,$cause) {

    $response = '<?xml version="1.0" encoding="utf-8"?>';
    $response .= '<response><status>'.$type.'</status>';

            $response = $response.'<remarks>'.$cause.'</remarks></response>';
            return $response;
 }

 ....
 ....

 header("Content-type: text/xml; charset=utf-8");
 echo sendResponse($type,$cause);

Please help if you can. Thank's in Advance, SX

S X
  • 19
  • 1
  • 7

2 Answers2

0

I'm not sure to understand your request, but first you can't call sendResponse() cause it's not a function but a method in a Class

You need to instantiate your class and after, call the method.

Example :

$yourObject = new YourClass();

$yourObject->sendResponse();

See the manual

For your case, see the manual for simpleXML and try that :

    function sendResponse($type,$cause) {

    $response = '<?xml version="1.0" encoding="utf-8"?>';
    $response .= '<response><status>'.$type.'</status>';

            $response = $response.'<remarks>'.$cause.'</remarks></response>';
            return $response;
 }

$type="type";
$cause="cause";
var_dump(simplexml_load_string(sendResponse($type,$cause)));

If your script is external, you can get it with file_get_contents

$xml = file_get_contents('http://yourTarget.com');

var_dump($xml);
Xenofexs
  • 511
  • 4
  • 14
  • May question is i need a pHp file to call an XML file and return the data, i need for a kodi addon...So the users can not get my XML file...I understand i need the php to return the data and then i need the XML file to restrict it on the server...sorry for confusion...Just the above example was just a copy paste from a forum, if theres a better way please if possible let me know. THank you. – S X Feb 04 '16 at 17:03
  • So, you have a extern xml and you wanna get it and parse the XML with php ? You can get the extern file with file_get_contents (http://php.net/manual/en/function.file-get-contents.php) and parse it with simplexml_load_string :) – Xenofexs Feb 04 '16 at 17:11
  • I want this code http://www.WEBSITEHERE.com/getxml.php // THIS IS THE CODE; – S X Feb 06 '16 at 22:30
  • BUT...when i call it via http://www.WEBSITE.com/getxml.php?username=newyork&pwd=newyork1 ...I do not want it to SHOW the .XML file...Which in thise case right now if you put this code into the browser will go directly to the .XML and anyone is able to view it.,,I tried IndexIgnore *.php and even the whole page into .htaccess but when you put that code into the browser goes directly to the .XML and it is viable...Is there a way i can disable that? .....? THank's – S X Feb 06 '16 at 22:35
0

You question misleads me to some extent, make sure if you want to load an external xml file and make your PHP code parse it. Try the following code

Assume the following is your xml content fo text.xml 

<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>"

<-- PHP -->

$file = 'http://exmp.com/text.xml';     // give the path of the external xml file
if(!$xml = simplexml_load_file($file))    // this checks whether the file exists
exit('Failed to open '.$file);           // exit when the path is wrong 
print_r($xml);                          // prints the xml format in the form of array 

your output will be this

SimpleXMLElement Object ( 
[to] => Tove 
[from] => Jani 
[heading] => Reminder 
[body] => Dont forget me this weekend!
 ) 

Hope this helps...

vrn53593
  • 308
  • 1
  • 5