【问题标题】:Using Zend_Http_Client to send json POST使用 Zend_Http_Client 发送 json POST
【发布时间】:2014-10-21 16:54:12
【问题描述】:

我正在尝试使用以下命令将 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 请求方面还很陌生,因此我们将不胜感激。

【问题讨论】:

    标签: php json post


    【解决方案1】:

    尝试改变:

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

    到:

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

    或使用:

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

    【讨论】:

    • 使用 $client->setRawData($json, 'application/json');失败。然而,使用上面的两行是一种享受。谢谢!
    【解决方案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 - 我也会尝试您的建议,看看是否可以让我设置正确的类型

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 1970-01-01
      • 2014-06-25
      • 2022-06-14
      • 1970-01-01
      相关资源
      最近更新 更多