我打算用magento的产品来映射一个基于java的服务器,我个给它们两者发送了相同的参数,但是调用php却出现了一下的错误:
'No content to map to Object due to end of input'
我的curl如下所示:
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();
这个问题该如何解决?如果有知道的,请告诉我一下,我讲不胜感激。
解决方案:
你前面的工作都是对的,但是在最后的代码有错误,果你把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;
}
我同时也修改了我的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();
这就是我解决这个问题的方法,可能有点复杂,但是我也只知道这种了,如果你知道了有什么更好的方法解决这个问题,请告诉我。
更多信息请访问麦进斗(www.maijindou.com)