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

    Join Date
    Mar 2004
    Location
    Nobbies beach, Gold Coast. It's beautiful.
    Posts
    2,574
    Rep Power
    171

    View response as xml


    Hello.

    Hi can I get request.php to show the response as xml?

    request.php
    PHP Code:
     header("Content-type: text/xml; charset=utf-8"); 
    $url 'http://www.webmoosh.com/aashghalduni/REST/responce_post.php';
    $data = array('id' => 5'name' => 'behnam');
    $ch curl_init();
    curl_setopt($chCURLOPT_URL$url);
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_POSTtrue);
    curl_setopt($chCURLOPT_POSTFIELDS$data);
    $response curl_exec($ch);
    $xml simplexml_load_string($response); 
    curl_close($ch);
    print_r($xml); 
    response.php
    PHP Code:
    if($_POST['id']==5)
        {
            $xml = "<?xml version=\"1.0\"?><responce><msg id='".$_POST['id']."'>pass</msg><name>".$_POST['name']."</name></responce>";
        }
    else
        {
            
    $xml "<?xml version=\"1.0\"?><responce><msg id='".$_POST['id']."'>failed</msg><name>".$_POST['name']."</name></responce>";
        }
    echo 
    $xml;
    So far I get this back (so I need a way to convert it to xml):
    Code:
    SimpleXMLElement Object
    (
        [msg] => pass
        [name] => behnam
    )
    Thanks
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,918
    Rep Power
    1045
    Hi,

    why do you need to parse an XML document just to generate the same document again?

    When $response already is the document you want, why can't you just output it?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Nobbies beach, Gold Coast. It's beautiful.
    Posts
    2,574
    Rep Power
    171
    Hi Jacques1,

    I believe I got things to work properly now
    Do you find any problems in this or do you find it secure enough and reliable way of generating xml response based on authentication result?
    PHP Code:
    <?php header("Content-type: text/xml; charset=utf-8"); 
    $url 'https://www.goldcoast-flatmates.com/REST/responce_post.php';
    $data = array('user'=>'valid_user''password'=>'password''city' => 'GOLD COAST');
    $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);?>
    responce_post.php
    PHP Code:
    <?php
    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>&& $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==&& $authenticate==TRUE)
        {
            
    $xml .="<root><results>No results Found!</results></root>";    
        }
        
    if(
    $authenticate==FALSE)
        {
            
    $xml .="<root><results>Invalid user</results></root>";    
        }
        echo 
    $xml;
    Last edited by zxcvbnm; March 27th, 2013 at 08:29 PM.

IMN logo majestic logo threadwatch logo seochat tools logo