Announcement

Collapse
No announcement yet.

Convert JSON to global variables from cURL request

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Convert JSON to global variables from cURL request

    I am writing a php script that makes a cURL POST request to a miva page.

    I would like to convert the request parameters (args) to global variables.

    Is there a Miva function that can do this?

    Here is php script:

    Code:
    <?php
        error_reporting(E_ERROR | E_PARSE | E_NOTICE | E_WARNING);
        require_once('lib/init.php');
        // header("Access-Control-Allow-Origin: *");
    
        $params = array(
                    'Product_Code' => 'some_code',
                    'Quantity' => '1'
                );
    
        $str_params = json_encode($params);
    
        $html_params = http_build_query($params);
    
    
        $url = "https://www.mydomain.com/path/add-to-cart.html";    
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER,
                array("Content-type: application/json"));
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $html_params); // use for html params
        // curl_setopt($curl, CURLOPT_POSTFIELDS, $str_params); toggle to use json params
    
        $json_response = curl_exec($curl);
    
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    
        if ( $status != 200 ) {
            die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
        }
    
        curl_close($curl);
    
        echo ('<!doctype html><html><head>');
        echo ('</head><body>');
        echo ('<h2>Details'.$html_params.'</h2>');
        echo ('<h2>Details'.$str_params.'</h2>');
        echo ('<h2>Details'.$json_response.'</h2>');
        echo ('</body></html>');
    
    ?>
    Here is the page template:

    Code:
    <mvt:do file="g.module_merchant" name="g.adpr_callback" value="Action_AddProductToBasket()" />
    
    {"type":"Success",
     "text":"JSON Received",
     "adpr_callback":"&mvt:global:adpr_callback;",
     "Quantity":"&mvt:global:quantity;",
     "Product_Code":"&mvt:global:product_code;"
     }
    From different testing that I have done the global quantity and product_code variable work when I enter it into the address bar but not when it is requested through cURL.

    I can echo the cURL html_parmas, json_params and json_reponse and I know they are being posted correctly from the php function.

    is there an mvt:do miva function that can convert query_string to json data variables?





    http://www.alphabetsigns.com/

    #2
    I found my mistake was a malformed POST request in cURL.

    I needed to change:

    Content-type: application/x-www-form-urlencoded

    CURL_OPTHEADER to true.
    http://www.alphabetsigns.com/

    Comment

    Working...
    X