6

I'm trying to send a json object as a POST command using the following:

    $uri = "http://amore-luce.com/product_create";
    $product =  $observer->getEvent()->getProduct();
    $json = Mage::helper('core')->jsonEncode($product);
    Mage::log(" Json={$json}", null,'product-updates.txt');

    // new HTTP request to some HTTP address
    $client = new Zend_Http_Client('http://amore-luce.com/product_create');
    // set some parameters
    $client->setParameterPost('product', $json);
    // POST request
    $response = $client->request(Zend_Http_Client::POST);

When I view the $json the data is there and all looks good - however the POST is not sending the json data. I'm capturing it using a simple email form that should send me the response:

    <?php        
    $webhookContent = "";
    $ref = "";       
    $webhook = fopen('php://input' , 'rb');
    while (!feof($webhook)) {
        $webhookContent .= fread($webhook, 4096);
    }
    fclose($webhook);

     $headers = array();
        foreach($_SERVER as $key => $value) {
            if (substr($key, 0, 5) <> 'HTTP_') {
                continue;
            }
            $header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
            $headers[$header] = $value;
        }

    foreach ($headers as $header => $value) {
        $ref .= "$header: $value <br />\n";
    }

    $post = file_get_contents('php://input');
    $to = "[email protected]"; //the address the email is being sent to
    $subject = "This is the subject"; //the subject of the message
    $msg = "This is the message  -  Webhook content:  ".$webhookContent."    This is url:  ".$ref; //the message of the email

    mail($to, $subject, $msg, 'From: PHP Scriptv2 <[email protected]>'); //send the email.

    echo ($_SERVER['HTTP_REFERER']."<br>".$_SERVER['REQUEST_URI']);
    ?>

This page works with other json POST requests I've sent to it. I'm fairly new at sending POST requests so any help would be greatly appreciated.

2 Answers 2

9

Try change :

$client->setParameterPost('product', $json);

to :

$client->setHeaders('Content-type','application/json');
$client->setParameterPost('product', $json);

or use:

$client->setRawData($json, 'application/json');
Sign up to request clarification or add additional context in comments.

1 Comment

Using $client->setRawData($json, 'application/json'); fails. However using the two lines above works a treat. Thank you!
2

So Update / Answer changing how I did it to these lines of code:

    $uri = "http://requestb.in/p6p4syp6";
    $product =  $observer->getEvent()->getProduct();
    $json = Mage::helper('core')->jsonEncode($product);
    Mage::log(" Json={$json}", null,'product-updates.txt');

    $client = new Zend_Http_Client($uri);
    $client->setRawData($json, null)->request('POST');

Changing the SetRawData($json, null) was the bit - using SetRawDate($json, 'application/json') caused it to fail.

Thanks Voodoo417 - I'll also try your suggestion and see if that lets me set the correct type

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.