dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Return cUrl'd data as a SimpleXML String

  1. #1
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    700

    Return cUrl'd data as a SimpleXML String

    I tried a few methods but couldn't get it to work,
    I am sending a post via curl for query data so can't
    simplexml_load_string directly to the url. Posted
    content is formed as xml, response is xml, so I'd
    like to just load as SXML and return it, but I'm
    evidently doing something wrong, here's the curl
    function.

    PHP Code:
    function worldvision_curlcall($dest$payload){
        
    $curl curl_init();
        
    curl_setopt($curlCURLOPT_URL$dest);
        
    curl_setopt($curlCURLOPT_RETURNTRANSFER1);
        
    curl_setopt($curlCURLOPT_POST1);
        
    curl_setopt($curlCURLOPT_POSTFIELDS'payload=' $payload);


        
    $data simplexml_load_string(curl_exec($curl));
      

      if (!
    $data) {
            
    $data curl_error($curl);
        }
        
    curl_close($curl);
        return 
    $data;


  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,436
    I would separate the curl_exec() into a separate step, so that you can more easily differentiate between cURL errors and SimpleXML errors, something like:
    PHP Code:
    <?php
    // add $errors array as pass-by-reference arg
    function worldvision_curlcall($dest$payload, &$errors=array())
    {
       
    $curl curl_init();
       
    curl_setopt($curlCURLOPT_URL$dest);
       
    curl_setopt($curlCURLOPT_RETURNTRANSFER1);
       
    curl_setopt($curlCURLOPT_POST1);
       
    curl_setopt($curlCURLOPT_POSTFIELDS'payload=' $payload);
       
    $result curl_exec($curl);
       if (
    $result == false) { // deal with cURL failure here
          
    $errors[] = curl_error($curl);
          return 
    false;
       }
       
    curl_close($curl);
       
    libxml_use_internal_errors(true); // requires PHP 5.1+
       
    $data simplexml_load_string($result);
       if (
    $data == false) { // deal with XML error here
          
    $errors libxml_get_errors();
       }
       return 
    $data;
    }

    // USAGE:
    $xml worldvision_curlcall($foo$bar$errors);
    if(
    $xml == false) {
       
    print_r($errors); // or whatever you want to do with any error data
    }
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    700
    Cool beans, thanks nog you always write something good.

    I have a quick question I want to fly by you. This data will
    most likely be appended multiple times, (this is a first call
    to get basic data, afterwards I will be appending in image
    urls, full profiles, etc). Would it be easier/better to keep this
    as SXML, or convert to an array? How would you treat/utilize it?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,436
    It's probably easier to work with an array and possibly a bit faster, but that might be offset to some degree if you have to convert it back into XML at the end?
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles