通过zend framework 2.3调用Web服务时响应为空

时间:2014-08-21 12:02:13

标签: php curl zend-framework2

我是zendframework 2.3的新手。在我的应用程序中,我需要调用Web服务。我使用了类Zend \ Http \ Client()......一切都很好......但是响应是空的..它通过核心php在卷曲调用中工作

$request =
'<?xml version="1.0"?>' . "\n" .
'<request><login>email</login><password>password</password></request>';

$client = new Zend\Http\Client();
$adapter = new Zend\Http\Client\Adapter\Curl();
$adapter->setOptions(array(
'curloptions' => array(
    CURLOPT_POST => 1,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_POSTFIELDS => $request,
    CURLOPT_RETURNTRANSFER => 1
)
));

$client->setUri("https://xyz/getCountries");
$client->setAdapter($adapter);
$client->setMethod('POST');
$response= $client->send();

echo "<pre>\n";
echo htmlspecialchars($response);
echo "</pre>";

3 个答案:

答案 0 :(得分:3)

HTTP客户端请求不返回字符串,它们返回Zend\Http\Response个对象。要查看您可能想要的输出:

echo "<pre>\n";
echo htmlspecialchars($response->getBody());
echo "</pre>";

另外,我个人会尝试避免直接在适配器上设置选项。如果需要,它会使更换适配器变得更加困难。

答案 1 :(得分:1)

您可以直接使用Http Client设置CURLOPT_POST中的CURLOP_POSTFIELDSClient选项,而不是Adapter中的选项,就像这样:< / p>

$data = '<?xml version="1.0"?>' . "\n" .
        '<request><login>email</login><password>password</password></request>';

$client = new Zend\Http\Client('https://xyz/getCountries');
$client->setMethod('POST');
$client->setRawBody($data);
//set the adapter without CURLOPT_POST and CURLOP_POSTFIELDS
$client->setAdapter(new Curl());
$response = $client->send();

然后在输出中得到响应(您的代码错误,您应该使用$response->getBody()):

echo "<pre>\n";
echo htmlspecialchars($response->getBody());
echo "</pre>";

这是关于如何在Zf2中使用Curl Http Adapter的好post

希望这有帮助。

答案 2 :(得分:0)

$request =
'<?xml version="1.0"?>' . "\n" .
'<request><login>email</login><password>password</password></request>';

$client = new Zend\Http\Client();
$adapter = new Zend\Http\Client\Adapter\Curl();
$adapter->setOptions(array(
'curloptions' => array(
CURLOPT_POST => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => $request,
 CURLOPT_RETURNTRANSFER => 1
)
));

$client->setUri("https://xyz/getCountries");
$client->setAdapter($adapter);
$client->setMethod('POST');
$response= $client->send();

echo "<pre>\n";
echo htmlspecialchars($response->getBody());
echo "</pre>";