Problems creating a string of xml

Discussion in 'PHP' started by michaelh613, Mar 17, 2008.

  1. #1
    
    $plist = '<?xml version = "1.0"?>';
    $plist .= '<REQUEST_GROUP MISMOVersionID="2.1">';
    $plist .= '<REQUEST LoginAccountIdentifier="testuser"';
    $plist .= ' LoginAccountPassword="testpass">';
    //more data
    $plist .= '<_RESIDENCE _StreetAddress="10655 Birch St"';              
    $plist .= ' _City="Burbank"';     
    $plist .= ' _State="CA" _PostalCode="20906" />'; 
    
    Code (markup):
    It appears the data <_ appears but not the rest of th data when I echo $plist.
     
    michaelh613, Mar 17, 2008 IP
  2. Gordaen

    Gordaen Peon

    Messages:
    277
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you change that first line to just $plist = ''; does it work? The "<?" may be being interpreted as a script opening. Be sure you have on full error reporting (including notices) and let us know if any errors are popping up.

    You may also want to try building your XML tree using the DOM classes that are built in to PHP.
     
    Gordaen, Mar 17, 2008 IP
  3. firman01

    firman01 Well-Known Member

    Messages:
    155
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    165
    #3
    try simple hack with the <? ?>

    
    <?php
    $plist = '<\?xml version = "1.0"\?>';
    $plist = str_replace('\\','',$plist);
    $plist .= '<REQUEST_GROUP MISMOVersionID="2.1">';
    $plist .= '<REQUEST LoginAccountIdentifier="testuser"';
    $plist .= ' LoginAccountPassword="testpass">';
    //more data
    $plist .= '<_RESIDENCE _StreetAddress="10655 Birch St"';
    $plist .= ' _City="Burbank"';
    $plist .= ' _State="CA" _PostalCode="20906" />';
    
    echo $plist;
    ?>
    
    Code (markup):
     
    firman01, Mar 17, 2008 IP
  4. michaelh613

    michaelh613 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks I've been working on this and found a much better way to create the xml.

    
    <?php
    $dom = new DOMDocument('1.0');
    //create root element <Request_group> and append it to the document
    $REQUEST_GROUP = $dom->appendChild($dom->createElement('REQUEST_GROUP'));
    $REQUEST_GROUP->setAttribute('MISMOVersionID','2.1');
    
    $REQUEST=$REQUEST_GROUP->appendChild($dom->createElement('REQUEST'));
    $REQUEST->setAttribute('LoginAccountIdentifier','testuser');
    
    $KEY=$REQUEST->appendChild($dom->createElement('KEY'));
    $KEY->setAttribute('_NAME','MemberID');
    $KEY->setAttribute('_Value','100');
    
    $REQUEST_DATA=$REQUEST->appendChild($dom->createElement('REQUEST_DATA'));
    $CREDIT_REQUEST=$REQUEST_DATA->appendChild($dom->createElement('CREDIT_REQUEST'));
    $CREDIT_REQUEST->setAttribute('MISMOVersionID','2.1');
    
    $CREDIT_REQUEST_DATA=$CREDIT_REQUEST->appendChild($dom->createElement('CREDIT_REQUEST_DATA'));
    $CREDIT_REQUEST_DATA->setAttribute('CreditRequestID','CRReq0001');
    $CREDIT_REQUEST_DATA->setAttribute('BorrowerID','BorRec0001');
    $CREDIT_REQUEST_DATA->setAttribute('CreditReportRequestActionType','Submit');
    $CREDIT_REQUEST_DATA->setAttribute('CreditReportType','Consumer');
    $CREDIT_REQUEST_DATA->setAttribute('CreditRequestType','Individual');
    
    $CREDIT_REPOSITORY_INCLUDED=$CREDIT_REQUEST_DATA->appendChild($dom->createElement('CREDIT_REPOSITORY_INCLUDED'));
    $CREDIT_REPOSITORY_INCLUDED->setAttribute('_EquifaxIndicator','N');
    $CREDIT_REPOSITORY_INCLUDED->setAttribute('_ExperianIndicator','Y');
    $CREDIT_REPOSITORY_INCLUDED->setAttribute('_TransUnionIndicator','N');
    
    $LOAN_APPLICATION=$CREDIT_REQUEST->appendChild($dom->createElement('LOAN_APPLICATION'));
    $BORROWER=$LOAN_APPLICATION->appendChild($dom->createElement('BORROWER'));
    $BORROWER->setAttribute('BorrowerID','BorRec0001');
    $BORROWER->setAttribute('_FirstName','EVPAULA');
    $BORROWER->setAttribute('_MiddleName','');
    $BORROWER->setAttribute('_LastName','Consumer');
    $BORROWER->setAttribute('_SSN','376988419');
    
    
    $_RESIDENCE=$BORROWER->appendChild($dom->createElement('_RESIDENCE'));
    $_RESIDENCE->setAttribute('_StreetAddress','10655 Birch St');
    $_RESIDENCE->setAttribute('_City','Burbank');
    $_RESIDENCE->setAttribute('_State','Ca');
    $_RESIDENCE->setAttribute('_PostalCode','20906');
    
    $dom -> formatOutput = true;
    $result= $dom->saveXML();
    
     ?>
    
    Code (markup):
    Now I am working on figuring out why curl isn't sending it correctly. I'll post back in a little bit on t hat
     
    michaelh613, Mar 17, 2008 IP
  5. michaelh613

    michaelh613 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Still can't figure out how to get curl to properly post the xml. Any hints would be greatly appreciated.

    Currently my code looks like this

    
    <?php
    Require 'createquidataxml.php';
    $Result = simplexml_load_string($result);
    $xmlResult= $Result->asXML();
    $ch= curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.yourcomputerconsultant.com/test_data.php");      
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $test=$xmlResult);
     
    curl_exec($ch);
    curl_close($ch);
    
    ?>
    
    Code (markup):
    I have tried it with and without $xmlResult= $Result->asXML();

    Just hitting my head right now.
     
    michaelh613, Mar 18, 2008 IP
  6. firman01

    firman01 Well-Known Member

    Messages:
    155
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    165
    #6
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, [b]$test=$xmlResult[/b]);
    
    Code (markup):

    i think you need to specify the postfiled like this example:

    
    $data = array(
        'uname' => $user,
        'pass' => $pass,
        'op' => 'login',
        'submit' => 'Login'
    );
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
    Code (markup):
    this is equal to
    
    uname=$user&pass=$pass&op=login&subit=Login
    
    Code (markup):
     
    firman01, Mar 18, 2008 IP
  7. michaelh613

    michaelh613 Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7

    But thats not xml?

    How would I do that with an xml file I am trying to post?
     
    michaelh613, Mar 18, 2008 IP
  8. firman01

    firman01 Well-Known Member

    Messages:
    155
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    165
    #8
    firman01, Mar 19, 2008 IP