I am sending a HTTP GET request to the web service of an external company. This is coming back fine, but I am running into problems with the interpretation of what comes back. The response is sent in XML. I have tried to use SimpleXML (which I admit, I am not familiar with) and cannot figure it out. What I need to do is assign something received in the XML to a PHP variable and then echo it. The variable I need to assign is "SessionToken" to $sessionid. How do I do this?
Here is an example of my code:
error_reporting(E_ALL);
$request = 'http://ws.examplesite.com/test/test.asmx/TestFunction?Packet=<Packet><Email>test@example.com</Email><Password>testpassword</Password></Packet>';
$session = curl_init($request);
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($session);
curl_close($session);
$status_code = array();
preg_match('/\d\d\d/', $response, $status_code);
switch( $status_code[0] ) {
case 200:
break;
case 503:
die('Your call to Web Services failed and returned an HTTP status of 503. That means: Service unavailable. An internal problem prevented us from returning data to you.');
break;
case 403:
die('Your call to Web Services failed and returned an HTTP status of 403. That means: Forbidden. You do not have permission to access this resource, or are over your rate limit.');
break;
case 400:
die('Your call to Web Services failed and returned an HTTP status of 400. That means: Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML response.');
break;
default:
die('Your call to Web Services returned an unexpected HTTP status of:' . $status_code[0]);
}
if (!($xml = strstr($response, '<?xml'))) {
$xml = null;
}
echo $xml;
Here is an example of the response:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://ws.example.com/resumes/">
<Packet>
<Errors />
<SessionToken>1234567890</SessionToken>
</Packet>
</string>
How can I extract variables from XML and assign them to PHP variable?