Reputation: 8146
I am trying to establish a communication with two zf2
application via curl
.The required response is in xml
. So far I could establish the connection and xml
is returned as response.
The problem
The problem is that,I can't iterate on my xml response
.Whenever I var_dump and view source code of my $response->getContent
,I get as
string(142) "<?xml version="1.0" encoding="UTF-8"?>
<myxml>
<login>
<status>success</status>
<Err>None</Err>
</login>
</myxml>
"
and when I simply var_dump
my $response
,I get an object(Zend\Http\Response)#440
.
simplexml_load_string($response->getContent())
gives me a blank page.
Also print $data->asXML()
gives me Call to a member function asXML() on a non-object
error.What am I doing wrong here?
Curl request action
$request = new \Zend\Http\Request();
$request->getHeaders()->addHeaders([
'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'
]);
$request->setUri('http://localhost/app1/myaction');
$request->setMethod('POST'); //uncomment this if the POST is used
$request->getPost()->set('curl', 'true');
$request->getPost()->set('email', '[email protected]');
$request->getPost()->set('password', '2014');
$client = new Client;
$client->setAdapter("Zend\Http\Client\Adapter\Curl");
$response = $client->dispatch($request);
var_dump($response);//exit;
//$response = simplexml_load_string($response->getContent());
//echo $response;exit;
return $response;
Curl response action
$php_array=array(
'login'=>array(
'status'=>'failed','Err'=>'Unauthorised Access'
)
);
$Array2XML=new \xmlconverter\Arraytoxml;
$xml = $Array2XML->createXML('myxml', $php_array);
$xml = $xml->saveXML();
//echo $xml;exit;
$response = new \Zend\Http\Response();
$response->getHeaders()->addHeaderLine('Content-Type', 'text/xml; charset=utf-8');
$response->setContent($xml);
return $response;
The Array2XML
can be found here
Any ideas?
Upvotes: 1
Views: 768
Reputation: 198204
when I simply var_dump my $response,I get an object(Zend\Http\Response)#440
This is correct and it tells you the type of $response
.
simplexml_load_string($response->getContent()) gives me a blank page.
This is correct because despite this function can return a value expressible to an empty string, it does not create any output on it's own and therefore the blank page is to be expected.
Any ideas?
First of all you should formulate a proper problem statement with your question. All you say so far therein is to be expected, so your question is not clear at best.
Second you need to do proper error handling and do some safe programming:
$buffer = $response->getContent();
if (!is_string($buffer) || !strlen($buffer) || $buffer[0] !== '<') {
throw new RuntimeException('Need XML string, got %s', var_export($buffer, 1));
}
$xml = simplexml_load_string($buffer);
if (false === $xml) {
throw new RuntimeException('Unable to parse response string as XML');
}
Which is: For every parameter you get, validate it. For every function or method result you receive check the post-conditions. Before you call a function or method, check the pre-conditions for each parameter.
Log errors to file and handle uncaught exceptions.
As an additional idea: Drop the use of the array to XML function and replace it with a library that is maintained. In your case it's perhaps easier to just use SimpleXML your own to create the XML.
Upvotes: 1