views:

1453

answers:

2

So having managed to crack getting nusoap to poll the chemspider server for information however I get a response that will display using print_r but when using print will simply display Array.

My question is this really, how do I take the given response and turn it into a php array

The code for the nusoap client

<?php
require_once('../lib/nusoap.php');
$client = new nusoap_client('http://www.chemspider.com/Search.asmx?WSDL', 'wsdl');
$err = $client->getError();
if ($err) {
    echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}
$query = 'methanol';
$token = 'token';
$result = $client->call(SimpleSearch, array('query' => $query, 'token' => $token), array('return' => 'xsd:string'), "http://www.chemspider.com/SimpleSearch") ;
// Check for a fault
if ($client->fault) {
    echo '<h2>Fault</h2><pre>';
    print_r($result);
    echo '</pre>';
} else {
    // Check for errors
    $err = $client->getError();
    if ($err) {
 // Display the error
 echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
 // Display the result
 echo '<h2>Result</h2><pre>';
 print_r($result);
 echo '</pre>';
}
}



echo '<h2>Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>

This gives the following output (minus the debug information):

Result

Array
(
    [SimpleSearchResult] => Array
        (
           [int] => 864
        )

 )

Request

POST /Search.asmx HTTP/1.0
Host: www.chemspider.com
User-Agent: NuSOAP/0.7.3 (1.114)
Content-Type: text/xml; charset=ISO-8859-1
SOAPAction: "http://www.chemspider.com/SimpleSearch"
Content-Length: 489

<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns8284="Array"><SOAP-ENV:Body><SimpleSearch xmlns="http://www.chemspider.com/"&gt;&lt;query&gt;methanol&lt;/query&gt;&lt;token&gt;6a0562f9-b416-40e5-a5c4-60c507827b3d&lt;/token&gt;&lt;/SimpleSearch&gt;&lt;/SOAP-ENV:Body&gt;&lt;/SOAP-ENV:Envelope&gt;

Response

HTTP/1.1 200 OK
x-cspc-fd: search.asmx
x-cspc-fh: chemspider
x-orig-path: /Search.asmx
Set-Cookie: x-dsp=
Set-Cookie: x-d-ond=dond
Set-Cookie: X-Mapping-kckcchol=47DE43E9D82204D9CDBBD4A2610306B8; path=/
Cache-Control: private, max-age=0
x-cspc-pl: 0
Content-Length: 381
x-cspc-hs: chemspider.com
Date: Thu, 24 Sep 2009 08:54:01 GMT
 x-bwcc: pub
 x-dsp: [][]
Connection: close
X-AspNet-Version: 2.0.50727
x-cspc-pt: /Search.asmx
Z-Spider: Hunstman-32-1
x-orig-host: chemspider.com
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Content-Type: text/xml; charset=utf-8

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;&lt;soap:Body&gt;&lt;SimpleSearchResponse xmlns="http://www.chemspider.com/"&gt;&lt;SimpleSearchResult&gt;&lt;int&gt;864&lt;/int&gt;&lt;/SimpleSearchResult&gt;&lt;/SimpleSearchResponse&gt;&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;
A: 

you want to parse the $client->response ? try explode ('\n', $client->response) and explode(': ', $each_string)

valya
Thank you for the suggestion though it turns out that the suggestion below fixed the problem. Many Thanks for trying though
Andrew Bailey
A: 

In the script you use, the response has already been parsed to a PHP array called $result. You've already got the array printed in the output as well.

Your issue is, as far as I can see, the fact that print() and echo() do not work well on arrays. They simply output the type (Array) instead of the contents.

You can call the output of $result with

print $result['simpleSearchResult']['int'] // Will display 864

You can read more on PHP's array handling in the PHP Manual.

Duroth