#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171

    Generating xml from database


    Hi;

    Is this proper way of sending xml request and generating response?
    PHP Code:
    header("Content-type: text/xml; charset=utf-8"); 
    $url 'https://www.sharenet.com/REST/responce_post.php';
    $data = array('user'=>'valid_user''password'=>'password''city' => 'NY');
    $ch curl_init();
    curl_setopt($chCURLOPT_URL$url);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_POSTtrue);
    curl_setopt($chCURLOPT_POSTFIELDS$data);
    echo 
    $response curl_exec($ch);
    curl_close($ch); 
    Response
    PHP Code:
    if($_POST['user']=='valid_user' && $_POST['password']=='password')
        {
            $authenticate = TRUE;
        }
    else
        {
            $authenticate = FALSE;
        }
    $xml = '<?xml version="1.0" encoding="UTF-8"?>';

    include "config.php";
    include "database_class.php";
    $sql = "SELECT place_name2,id,  title, weekly_rent, date_added FROM ad_have WHERE place_name2=:city LIMIT 300";    
    $args = array('city'=>$_POST['city']);
    $number_of_rows = DB::Load()->Execute($sql,$args)->returnNumAffectedRows();


    function xml_entities($string) {
        return strtr(
            $string, 
            array(
                "<" => "&lt;",
                ">" => "&gt;",
                '"' => "&quot;",
                "'" => "&apos;",
                "&" => "&amp;",
            )
        );
    }



    if($number_of_rows>0 && $authenticate==TRUE)
        {
            $data = DB::Load()->Execute($sql,$args)->returnArray();
            $xml .='<rentals>';
            foreach($data as $val=>$row)
                {
                    $xml.= "<property id=\"".$row['id']."\">";
                    $xml.= "<title>".xml_entities($row['title'])."</title>";
                    $xml.= "<weekly_rent>".$row['weekly_rent']."</weekly_rent>";
                    $xml.= "<date_added>".$row['date_added']."</date_added>";
                    $xml.= "</property>";
                }
            $xml.="</rentals>";
        }
    if($number_of_rows==0 && $authenticate==TRUE)
        {
            $xml .="<root><results>No results Found!</results></root>";    
        }
        
    if($authenticate==FALSE)
        {
            $xml .="<root><results>Invalid user</results></root>";    
        }
        echo $xml;
    Thanks
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,995
    Rep Power
    9397
    That's not an XML request, and as long as the response XML is valid then sure that part's fine.

    Well, fine except for you escaping apostrophes. Don't: XML does not have the &amp;apos; entity. Use htmlspecialchars() without ENT_QUOTES and avoid (ie, continue avoiding) using apostrophes as part of the XML markup.
  4. #3
  5. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    An XML request uses XML encoded data in the post body, not URL encoded data as you would normally find....this also means that you won't have a $_POST array populated as you expect.

    To handle an XML request you would need to read in the raw request with, eg

    PHP Code:
    $xml_str file_get_contents("php://input"); 
    you would then need to parse the xml (eg $xml_str) with simpleXml, DOMDocument or similar to extract the data
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

IMN logo majestic logo threadwatch logo seochat tools logo