CURL post请求工作,而PHP(Magento)Varien_Http_Client不 [英] CURL post request works whereas PHP(Magento) Varien_Http_Client does not

查看:311
本文介绍了CURL post请求工作,而PHP(Magento)Varien_Http_Client不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 '由于输入结束,没有内容映射到对象

我的curl如下,

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

PHP请求的代码如下,

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

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

解决方案



好吧,终于这个工作。如果你引用 Zend_Http_Client ,问题是在我的代码结束时执行错误。请参考以下方法从Zend_Http_Client,

  / ** 
*为请求设置POST参数。包装在_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;
}

/ **
*设置GET或POST参数 - 由SetParameterGet和SetParameterPost使用
*
* @param string $ type GET或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
'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();

这解决了这个问题。我不知道一个更好的方法,但如果有什么更好会很高兴使用它。


I am trying to call a Java based service that uses a Jackson object mapper from a PHP(Magento) application. In both of them I am sending the same headers and same parameters, but the CURL call works fine where as PHP call fails with following message,

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

My curl is as follows,

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/

The PHP Request is code is as follows,

                 $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();

I am not the owner of the java service that uses jackson object mapper so I have no idea what happens on other side

Any suggestions on debugging or fixing this would be appreciated

解决方案

Well, finally this worked. The issue was with the wrong implementation in my end of the code, if you refer the Zend_Http_Client. Please refer to the methods below from 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;
    }
}

So the setParameterPost somehow honors only the array parameters(key value pairs) and my POST payload was a json string. So to solve the issue, I modified the code as below,

$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();

This solved the issue. I am not sure of a better way, but if there is anything better would be glad to use it.

这篇关于CURL post请求工作,而PHP(Magento)Varien_Http_Client不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆