我尝试使用以下命令将json对象作为POST命令发送:
$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);
当我查看$ json数据时,所有数据都很好 - 但是POST没有发送json数据。我使用一个简单的电子邮件表单捕获它,该表单应该向我发送响应:
<?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 = "address@my-email.com"; //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 <noreply@domain.com>'); //send the email.
echo ($_SERVER['HTTP_REFERER']."<br>".$_SERVER['REQUEST_URI']);
?>
此页面适用于我发送给它的其他json POST请求。我发送POST请求时相当新,所以我们非常感谢任何帮助。
答案 0 :(得分:6)
尝试更改:
$client->setParameterPost('product', $json);
到:
$client->setHeaders('Content-type','application/json');
$client->setParameterPost('product', $json);
或使用:
$client->setRawData($json, 'application/json');
答案 1 :(得分:2)
所以更新/回答改变了我对这些代码行的处理方式:
$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');
改变SetRawData($ json,null)是有点 - 使用SetRawDate($ json,'application / json')导致它失败。
感谢Voodoo417 - 我也会尝试你的建议,看看是否能让我设置正确的类型