gpt4 book ai didi

php - 使用 Zend_Http_Client 发送 json POST

转载 作者:可可西里 更新时间:2023-11-01 13:50:06 27 4
gpt4 key购买 nike

我正在尝试使用以下命令将 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 请求方面相当陌生,因此非常感谢任何帮助。

最佳答案

尝试改变:

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

到:

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

或使用:

$client->setRawData($json, 'application/json');

关于php - 使用 Zend_Http_Client 发送 json POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25538440/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com