Php 使用Zend_Http_客户端发送json POST
我尝试使用以下命令将json对象作为POST命令发送:Php 使用Zend_Http_客户端发送json POST,php,json,post,Php,Json,Post,我尝试使用以下命令将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.
$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时,数据就在那里,看起来一切都很好——但是帖子没有发送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请求相当陌生,因此非常感谢您的帮助。尝试更改:
$client->setParameterPost('product', $json);
致:
或使用:
$client->setRawData($json, 'application/json');
因此,更新/回答更改我对这些代码行的执行方式:
$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-我也会尝试你的建议,看看这是否能让我使用$client->setRawData($json,'application/json')设置正确的类型;失败。然而,使用上面的两行代码是一种享受。非常感谢。
$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');