Interact with XML API via POST

Discussion in 'PHP' started by gthing, Jan 12, 2011.

  1. #1
    I am attempting to interact with an API that requires sending and receiving an XML packet. The instruction in the API doc simply says:

    I've attempted to do this with various errors. I'm not sure if I'm even going about it in the right way. Here is my code:
    	/** 
         * Define POST URL and also payload
         */
         $payload = 'foo_xml_input=<?xml version="1.0" encoding="UTF-8"?><foo username="uaer" password="pass" api="1"/>';
        define('XML_SEND', $payload);
        define('XML_POST_URL', 'http://api.foo.com/api/getSiteIDs.cfm');
    
        /**
         * Initialize handle and set options
         */
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, XML_POST_URL); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_TIMEOUT, 4); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, XML_SEND); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
        
        /**
         * Execute the locarequest and also time the transaction
         */
        $start = array_sum(explode(' ', microtime()));
        $result = curl_exec($ch); 
        $stop = array_sum(explode(' ', microtime()));
        $totalTime = $stop - $start;
    Code (markup):
    http://pastebin.com/909bgcUE

    Here is the current error I am getting:

    An error occured while Parsing an XML document. The entity name must immediately follow the '&' in the entity reference.
    Code (markup):
    Any help on what I might be doing wrong would be appreciated.
     
    gthing, Jan 12, 2011 IP
  2. QiSoftware

    QiSoftware Well-Known Member

    Messages:
    805
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    158
    #2
    I would search google for an xmlrpc curl example.

    Q...
     
    QiSoftware, Jan 12, 2011 IP
  3. gthing

    gthing Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, I did that and ended up with the code you see above. But it doesn't work. When the API doc says "Wrap the following XML structure in a variable called ..." I assume it means a POST variable, but it doesn't specify further. I was hoping someone with experience working with APIs that communicate via XML would be able to recognize the problem.
     
    gthing, Jan 12, 2011 IP
  4. gthing

    gthing Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Blast! They had an error on their end. They finally looked into it after several days of banging my head against the wall and fixed it. Everything works great now.
     
    gthing, Jan 14, 2011 IP