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
    695

    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
    19,157
    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
    }
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2008
    Location
    Seattle, Wa
    Posts
    695
    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
    19,157
    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?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

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