Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Zend_Http_Client to send json POST

Tags:

json

post

php

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.

like image 866
Peter Fletcher Avatar asked Aug 27 '14 23:08

Peter Fletcher


1 Answers

Try change :

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

to :

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

or use:

$client->setRawData($json, 'application/json');
like image 102
voodoo417 Avatar answered Oct 22 '22 11:10

voodoo417