We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48586
    • 78 Posts
    Hello, I have a snippet that (will be) taking a data stream from another server in our company, converting to json, and outputting the data to a chunk. When I hard code the data stream and set as a variable it works perfectly, but now that we have resolved some firewall issues I am able to connect to the other server to get live data, and I'm not sure how to update my snippet to pull in the data.

    From ssh, when I type:

    curl -H "Authorization: Token token=abcdefghijklmnop" -H "Accept: application/json" 'http://www.example.com/whatever/path/to/stream'


    I get the exact format of the code I need

    However (there is always a "however"), cURL may be installed on my server, but it is not part of the php setup for the site. I have begged, pleaded, and tried bribing our system admins to set up cURL on my site, but it just isn't going to happen any time soon, or ever.

    How can I code my snippet to return the data from 'http://www.example.com/whatever/path/to/stream' to use on my site?

    For example (this is what I have that is working):

    $datastream = json_decode(<<<HEREDOC
    [{"id":54,"mkt_id":5,"offer_category":8,"offer_status":"T",
    blah blah blah, boring details
    etc etc
    HEREDOC
    );
    
    str_replace("'", "\'", $datastream);
    
    $data = array();
    
    foreach($datastream as $component) {
    
    ... and so on for values to my chunk
    


    Thank you very much in advance for any help

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

      • 3749
      • 24,544 Posts
      See if this works (or something similar):

      $data = json_decode(stripslashes(file_get_contents("php://input")), true);


      I've also used this to get just the values from an associative array into an array with numeric keys:

      $data = array_values(json_decode(stripslashes(file_get_contents("php://input")), true));


      For security, be sure to sanitize the data before doing anything with it.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 48586
        • 78 Posts
        Hi Bob, thank you for your reply, I will definitely try that to shorten the code, but my first issue is being able to connect to the data source. It is behind a user/pass, and to be able to access it directly to provide the data for my site, I have to set 2 headers:

        header("Authorization: Token token=abcdefghijklmnop");
        header("Accept: application/json");
        
        $url = 'http://www.example.com/whatever/path/to/stream';


        I'm not sure if I have that set up correctly, or am I missing some MODX magic to be able to set the header from a snippet?
        • discuss.answer
          • 3749
          • 24,544 Posts
          Sorry, I thought an Ajax call was sending the data to your snippet.

          See if anything here helps: http://stackoverflow.com/questions/2945887/how-to-get-a-webpages-contents-without-curl

          Some other ideas here: http://stackoverflow.com/questions/2822870/alternative-to-curl-due-to-long-waiting

          There's a way to set headers for use with file_get_contents() (which can take a URL as its argument), but the examples I've found aren't very clear about how to set multiple headers.

            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 48586
            • 78 Posts
            Thank you, that was the missing puzzle piece I needed. Here is the final code I used, and it is working great!

            <?php
            
            $context = stream_context_create(array(
                'http' => array(
                    'method' => 'GET',
                    'header' => "Authorization: Token token=longstringauthorizationtoken\r\n" .
                                "Accept: application/json\r\n"
                )
            ));
            $result = file_get_contents("http://www.example.com/path/to/feed", false, $context);
            
            //var_dump($result);
            
            $datastream = json_decode($result);
            
            //str_replace("'", "\'", $datastream);
            
            $data = array();
            
            foreach($datastream as $component) {
                
                $productid = $component->id;
                $service_category_codes = $component->service_category_codes;
            
                ... and more arrays and nested arrays being called from the data stream
            
            if ($service_category_codes == array("S")) {
                    $output_chunk = 'datastream_s_chunk';
                } elseif ($service_category_codes == array("O")) {
                    $output_chunk = 'datastream_o_chunk';
                } elseif ($service_category_codes == array("C", "K")) {
                    $output_chunk = 'datastream_ck_chunk';
                };
                
                $outputarray = array('productid' => $productid,
                'offer_headline' => $offer_headline,
                etc etc);
                
                $output .= $modx->getChunk($output_chunk, $outputarray);
            }
            
            return $output;
              • 3749
              • 24,544 Posts
              I'm glad I could help. Thanks for posting the code. smiley
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting