【问题标题】:CURL post request works whereas PHP(Magento) Varien_Http_Client does notCURL 发布请求有效,而 PHP(Magento) Varien_Http_Client 无效
【发布时间】:2013-02-28 10:50:02
【问题描述】:

我正在尝试调用基于 Java 的服务,该服务使用 PHP(Magento) 应用程序中的 Jackson 对象映射器。在他们两个中,我都发送相同的标头和相同的参数,但是 CURL 调用工作正常,因为 PHP 调用失败并显示以下消息,

'No content to map to Object due to end of input'

我的卷曲如下,

curl -v -k -X POST -H "Content-Type:application/json;charset=UTF-8" -d '{"name":"john","email":"john@doe.com"}' https://localhost:8080/webapps/api/

PHP请求代码如下,

                 $iClient = new Varien_Http_Client();
                 $iClient->setUri('https://localhost:8080/webapps/api/')
        ->setMethod('POST')
        ->setConfig(array(
                'maxredirects'=>0,
                'timeout'=>30,
        ));
    $iClient->setHeaders($headers);
    $iClient->setParameterPost(json_encode(array(
                    "name"=>"John",
                    "email"=>"john@doe.com"
                    )));    
    $response = $iClient->request();

我不是使用 jackson 对象映射器的 java 服务的所有者,所以我不知道另一边会发生什么

任何有关调试或修复此问题的建议将不胜感激

【问题讨论】:

    标签: php json magento curl jackson


    【解决方案1】:

    嗯,终于成功了。如果您参考Zend_Http_Client,问题在于我的代码末尾的错误实现。请参考以下 Zend_Http_Client 中的方法,

    /**
     * Set a POST parameter for the request. Wrapper around _setParameter
     *
     * @param string|array $name
     * @param string $value
     * @return Zend_Http_Client
     */
    public function setParameterPost($name, $value = null)
    {
        if (is_array($name)) {
            foreach ($name as $k => $v)
                $this->_setParameter('POST', $k, $v);
        } else {
            $this->_setParameter('POST', $name, $value);
        }
    
        return $this;
    }
    
    /**
     * Set a GET or POST parameter - used by SetParameterGet and SetParameterPost
     *
     * @param string $type GET or POST
     * @param string $name
     * @param string $value
     * @return null
     */
    protected function _setParameter($type, $name, $value)
    {
        $parray = array();
        $type = strtolower($type);
        switch ($type) {
            case 'get':
                $parray = &$this->paramsGet;
                break;
            case 'post':
                $parray = &$this->paramsPost;
                break;
        }
    
        if ($value === null) {
            if (isset($parray[$name])) unset($parray[$name]);
        } else {
            $parray[$name] = $value;
        }
    }
    

    所以 setParameterPost 以某种方式只尊重数组参数(键值对),而我的 POST 有效负载是一个 json 字符串。所以为了解决这个问题,我修改了如下代码,

    $iClient = new Varien_Http_Client();
                 $iClient->setUri('https://localhost:8080/webapps/api/')
        ->setMethod('POST')
        ->setConfig(array(
                'maxredirects'=>0,
                'timeout'=>30,
        ));
    $iClient->setHeaders($headers);
    $iClient->setRawData(json_encode(array(
                    "name"=>"John",
                    "email"=>"john@doe.com"
                    )), "application/json;charset=UTF-8");    
    $response = $iClient->request();
    

    这解决了这个问题。我不确定更好的方法,但如果有更好的方法,我会很高兴使用它。

    【讨论】:

      猜你喜欢
      • 2014-09-18
      • 2023-03-19
      • 1970-01-01
      • 2018-10-27
      • 2018-10-22
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 2022-11-29
      相关资源
      最近更新 更多