We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46577
    • 54 Posts
    I'm getting xml with a curl call to a remote server and would like to use the $modx->getChunk method to display results on a page, but can't get it to work.

    here's the last few lines of the curl call:
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
    $response = curl_exec($ch);
    curl_close($ch);
    var_dump($response);


    which outputs something like:

    string(332) "<?xml version="1.0" encoding="UTF-8"?>
    <xml>
    <messages recipientcount="1">
    <message>
    <to>00000000</to>
    <messageid>FB2FF641</messageid>
    <errortext>Success</errortext>
    </message>
    </messages>
    </xml>"

    so I change the code to look something like this (and a number of different variations along these lines):
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
    $response = curl_exec($ch);
    curl_close($ch);
    $output = $modx->getChunk('test', array(
    'messageid' => (string)$response -> messages -> message -> messageid
    ));
    return $output;
    


    In my testMessage chunk I want to have [[+messageid]] to display the messageid, but nothing is displayed

    Seems like I just need to get the syntax right, but I can't find the solution. Any suggestions greatly appreciated.

    This question has been answered by grogorio. See the first response.

    [ed. note: grogorio last edited this post 5 years, 8 months ago.]
      • 44064
      • 185 Posts
      Hi,
      First of all why application/x-www-form-urlencoded instead application/xml?

      Maybe this will help you:
      $response = curl_exec($ch);
      curl_close($ch);
      $xml = simplexml_load_string($response);
      foreach ($xml->messages as $message) {
      //...do something
      }
      
      
        Anton Tarasov
        MODX Developer

        Email: [email protected]
        Web: antontarasov.com
        • 46577
        • 54 Posts
        Thanks for you assistance himurovich.

        It took me a while working on the '...do something' part but I came up with this working example:

         {
        $to = ($messages->message->to);
        $messageid = ($messages->message->messageid);
        $errortext = ($messages->message->errortext);
        $output .= $to.'<br>'.$messageid.'<br>'.$errortext;
        }
        return $output;
        


        Still no joy getting it to work with a $modx->getChunk call. I tried a few variations of this:
        {
        $output .= $modx->getChunk('test', $messages);
        }
        


        then in my chunk:
        [[+messages.message.to]]<br>
        [[+messages.message.errortext]]<br>
        [[+messages.message.messageid]]
        


        but not getting any output. I can live with what I have and expand on the snippet, but it would be nice to do it via output modifiers from within the chunk. [ed. note: grogorio last edited this post 5 years, 8 months ago.]
          • 46577
          • 54 Posts
          BTW himurovich with regards to your question:
          First of all why application/x-www-form-urlencoded instead application/xml?
          I can't really give you a definitive answer as the curl connection script is supplied by the service provider. However if I use application/xml, the server responds with "Invalid credentials". [ed. note: grogorio last edited this post 5 years, 8 months ago.]
          • discuss.answer
            • 46577
            • 54 Posts
            I found a solution here https://stackoverflow.com/questions/23991349/templating-a-snippet-with-modx-getchunk-returns-nothing

            For my case, the following worked:

            foreach ($xml->messages as $messages) {
            $output .= $modx->getChunk('test', array(
            'to' => (string)$messages->message->to,
            'messageid' => (string)$messages->message->messageid,
            'errortext' => (string)$messages->message->errortext
            ));
            }
            


            and in the chunk:

            [[+to]]<br>
            [[+errortext]]<br>
            [[+messageid]]
            


            hope it helps somebody (usually me when I've forgotten in a couple of years time how I did something) [ed. note: grogorio last edited this post 5 years, 8 months ago.]