Lorna book in adv php - in 3rd api ch. - includes a wsdl.xml file and client, but se

I am a reader of Lorna book in adv php - in 3rd api ch. - includes a wsdl.xml file and client, but seems function in wsdl no return anything, but in reality return (prior example return string — but now wsdl no return) can you provide wsdl that returns or tell how run function - just the example in wsdl show extract functions like: /// i tried but can not generate a wsdl

$functions = $client->__getFunctions();
var_dump($functions);

// getting >>>
array(2) { [0]=> string(35) “void countWords(anyType $paragraph)” [1]=> string(60) “void getDisplayName(anyType $first_name, anyType $last_name)” }

I’m guessing you mean
“PHP Master: Write Cutting-edge Code” ??

yes correct…

in wsdl script valid for function that returns value?

well, is it wsdl that return the functions value?

also if localhost(xampp) not support
curl_init
what do to activate?

in WSs last section of Rest code to run the Rest web service like p.118:


  $ch = curl_init('http://localhost/rest/events');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $response = curl_exec($ch);
  $events = json_decode($response,1);

i must have the folder of ch.3: /rest/ to root of localhost? or the contents of /rest/ directly to the root? or on live server to it domain root?
so with above i must echo the $events? and this snipet above can be in other domain than the rest web service?

index.php is called implicitly in REST ws?

Sorry, I’m having trouble understandng.

The wsdl file is like “instructions” or “rules”, it isn’t meant to “return something”. It’s an XML file used for data integrity

<?xml version='1.0' encoding='UTF-8'?>
<definitions name="SimpleWSDL" targetNamespace="urn:SimpleWSDL"
xmlns:typens="urn:SimpleWSDL" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://schemas.xmlsoap.org/wsdl/">
	<message name="countWords">
		<part name="paragraph" type="xsd:anyType"></part>
	</message>
	<message name="countWordsResponse"></message>
	<message name="getDisplayName">
		<part name="first_name" type="xsd:anyType"></part>
		<part name="last_name" type="xsd:anyType"></part>
	</message>
	<message name="getDisplayNameResponse"></message>
	<portType name="ServiceFunctionsPortType">
		<operation name="countWords">
			<input message="typens:countWords"></input>
			<output message="typens:countWordsResponse"></output>
		</operation>
		<operation name="getDisplayName">
			<input message="typens:getDisplayName"></input>
				<output message="typens:getDisplayNameResponse"></output>
		</operation>
	</portType>
	<binding name="ServiceFunctionsBinding"	type="typens:ServiceFunctionsPortType">
		<soap:binding style="rpc"	transport="http://schemas.xmlsoap.org/soap/http"></soap:binding>
		<operation name="countWords">
			<soap:operation soapAction="urn:ServiceFunctionsAction"></soap:operation>
			<input>
				<soap:body namespace="urn:SimpleWSDL" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
				</soap:body>
			</input>
			<output>
				<soap:body namespace="urn:SimpleWSDL" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
				</soap:body>
			</output>
		</operation>
		<operation name="getDisplayName">
			<soap:operation soapAction="urn:ServiceFunctionsAction"> </soap:operation>
			<input>
				<soap:body namespace="urn:SimpleWSDL" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
				</soap:body>
			</input>
			<output>
				<soap:body namespace="urn:SimpleWSDL" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
				</soap:body>
			</output>
		</operation>
	</binding>
	<service name="SimpleWSDLService">
		<port name="ServiceFunctionsPort" binding="typens:ServiceFunctionsBinding">
			<soap:address location="http://localhost/soap-server.php">
			</soap:address>
		</port>
	</service>
</definitions>

If you are having trouble with PHP, check your php.ini file, it should have a secion like

[soap]
; Enables or disables WSDL caching feature.
; http://php.net/soap.wsdl-cache-enabled
soap.wsdl_cache_enabled=1

; Sets the directory name where SOAP extension will put cache files.
; http://php.net/soap.wsdl-cache-dir
;soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_dir="C:/PHP/tmp"

; (time to live) Sets the number of second while cached file will be used
; instead of original one.
; http://php.net/soap.wsdl-cache-ttl
soap.wsdl_cache_ttl=86400

; Sets the size of the cache limit. (Max. number of WSDL files to cache)
soap.wsdl_cache_limit = 5

wsdl
This wsdl is the same as in the book? and came from class that its both methods return value?

In php.ini for wsdl to work must match above as you put in this thread from php.ini?

rest
i must have the folder of ch.3: /rest/ to root of localhost? or the contents of /rest/ directly to the root? or on live server to it domain root?
so with below code i must echo the $events? and this snippet above can be in other domain than the rest web service?
in WSs last section of Rest code to run the Rest web service like p.118:

  $ch = curl_init('http://localhost/rest/events');   [B] // http://localhost/rest/events  OR http://localhost/events[/B]
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $response = curl_exec($ch);
  $events = json_decode($response,1);

index.php is called implicitly in REST ws?

It’s been a while since I’ve worked with SOAP or REST and haved preferred JSON for a while.
I don’t have the book you’re working through, so that complicates things too.

In any case, I’m digging through my old files looking for my last working code for you to try. Hopefully I’ll find something helpful soon.

In the meantime, please read these as I think they should answer some of your questions.

I also forgot to mention that you will need to make sure your php.ini file has the soap dll line uncommented

extension=php_soap.dll

Finally found some old example files.

First thing you should do is, if you don’t already, put this in your server’s root and go to it in your browser

<?php
phpinfo();
?>

you should see something in it like

Then in a soap folder put
catalog.wsdl

<?xml version ='1.0' encoding ='UTF-8' ?>
<definitions name='Catalog'
  targetNamespace='http://example.org/catalog'
  xmlns:tns='http://example.org/catalog'
  xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
  xmlns:xsd='http://www.w3.org/2001/XMLSchema'
  xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
  xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
  xmlns='http://schemas.xmlsoap.org/wsdl/'>

<message name='getCatalogRequest'>
  <part name='catalogId' type='xsd:string'/>
</message>
<message name='getCatalogResponse'>
  <part name='Result' type='xsd:string'/>
</message>

<portType name='CatalogPortType'>
  <operation name='getCatalogEntry'>
    <input message='tns:getCatalogRequest'/>
    <output message='tns:getCatalogResponse'/>
  </operation>
</portType>

<binding name='CatalogBinding' type='tns:CatalogPortType'>
  <soap:binding style='rpc'
    transport='http://schemas.xmlsoap.org/soap/http'/>
  <operation name='getCatalogEntry'>
    <soap:operation soapAction='urn:localhost-catalog#getCatalogEntry'/>
    <input>
      <soap:body use='encoded' namespace='urn:localhost-catalog'
        encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
    </input>
    <output>
      <soap:body use='encoded' namespace='urn:localhost-catalog'
        encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
    </output>
  </operation>
</binding>

<service name='CatalogService'>
  <port name='CatalogPort' binding='CatalogBinding'>
    <soap:address location='http://localhost/soap/soap-server.php'/>
  </port>
</service>
</definitions>

soap-server.php

<?php
function getCatalogEntry($catalogId)
{
	if ($catalogId == 'catalog1')
	{
	$first = "<html><head><title>Catalog</title></head>
	<body>
	<table>
	<tr>
	<th>CatalogId</th>
	<th>Journal</th>
	<th>Section</th>
	<th>Edition</th>
	<th>Title</th>
	<th>Author</th>
	</tr>
	<tr>
	<td>catalog1</td>
	<td>IBM developerWorks</td>
	<td>XML</td>
	<td>October 2005</td>
	<td>JAXP validation</td>
	<td>Brett McLaughlin</td>
	</tr>
	</table>
	</body>
	</html>";
	return $first;
	}
	elseif ($catalogId == 'catalog2')
	{
	$second = "<html><head><title>Catalog</title></head>
	<body>
	<table>
	<tr>
	<th>CatalogId</th>
	<th>Journal</th>
	<th>Section</th>
	<th>Edition</th>
	<th>Title</th>
	<th>Author</th>
	</tr>
	<tr>
	<td>catalog2</td>
	<td>IBM developerWorks</td>
	<td>XML</td>
	<td>July 2006</td>
	<td>The Java XPath API</td>
	<td>Elliotte Harold</td>
	</tr>
	</table>
	</body>
	</html>";
	return $second;
	}
}
ini_set("soap.wsdl_cache_enabled", false);
$server = new SoapServer("http://localhost/soap/catalog.wsdl");
$server->addFunction("getCatalogEntry");
$server->handle();

?>

and
soap-client.php

<?php
try
{
  $client = new SoapClient("http://localhost/soap/catalog.wsdl");
  $catalogId = 'catalog1';
  $response = $client->getCatalogEntry($catalogId);
}
catch (SoapFault $fault)
{
    trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
}
  var_dump($response);
?>

If all is well, when you go to http://localhost/soap/soap-client.php you should see

your example succed thks but mine still getting not the result - i get:
array(2) { [0]=> string(35) “void countWords(anyType $paragraph)” [1]=> string(60) “void getDisplayName(anyType $first_name, anyType $last_name)” }

soap-client-live-wsdl-xml.php

<?php

ini_set('soap.wsdl_cache_enabled', 0);
$client = new SoapClient('http://localhost/VT/Learnable/OOP-PHP/spbooks-PHPPRO1-ae9bb56/spbooks-PHPPRO1-ae9bb56/chapter_03/wsdl.wsdl');
$functions = $client->__getFunctions();
var_dump($functions);
echo "<hr/>";
echo $client->getDisplayName('Joe', 'Bloggs');

wsdl.wsdl


<?xml version='1.0' encoding='UTF-8'?>
<definitions name="SimpleWSDL" targetNamespace="urn:SimpleWSDL" 
xmlns:typens="urn:SimpleWSDL" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
xmlns="http://schemas.xmlsoap.org/wsdl/">
  <message name="countWords"><part name="paragraph" 
type="xsd:anyType"></part></message>
  <message name="countWordsResponse"></message>
  <message name="getDisplayName"><part name="first_name" 
type="xsd:anyType"></part><part name="last_name" 
type="xsd:anyType"></part></message>
  <message name="getDisplayNameResponse"></message>
  <portType name="ServiceFunctionsPortType">
    <operation name="countWords"><input 
message="typens:countWords"></input><output 
message="typens:countWordsResponse"></output></operation>
    <operation name="getDisplayName"><input 
message="typens:getDisplayName"></input><output 
message="typens:getDisplayNameResponse"></output></operation>
  </portType>
  <binding name="ServiceFunctionsBinding" 
type="typens:ServiceFunctionsPortType"><soap:binding style="rpc" 
transport="http://schemas.xmlsoap.org/soap/http"></soap:binding>
    <operation name="countWords">
      <soap:operation soapAction="urn:ServiceFunctionsAction"></soap:operation>
      <input><soap:body namespace="urn:SimpleWSDL" use="encoded" 
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"></soap:body></input>
      <output><soap:body namespace="urn:SimpleWSDL" use="encoded" 
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"></soap:body></output>
    </operation>
    <operation name="getDisplayName">
      <soap:operation soapAction="urn:ServiceFunctionsAction"></soap:operation>
      <input><soap:body namespace="urn:SimpleWSDL" use="encoded" 
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"></soap:body></input>
      <output><soap:body namespace="urn:SimpleWSDL" use="encoded" 
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"></soap:body></output>
    </operation>
  </binding>
  <service name="SimpleWSDLService">
    <port name="ServiceFunctionsPort" 
binding="typens:ServiceFunctionsBinding"><soap:address location="http://localhost/VT/Learnable/OOP-PHP/spbooks-PHPPRO1-ae9bb56/spbooks-PHPPRO1-ae9bb56/chapter_03/soap-server-WSDL.php"></soap:address></port>
  </service>
</definitions>

soap-server-WSDL.php

<?php
include 'ServiceFunctions.php';
$options = array('uri' => 'http://localhost/');
ini_set("soap.wsdl_cache_enabled", false); 
$server = new SoapServer("http://localhost/VT/Learnable/OOP-PHP/spbooks-PHPPRO1-ae9bb56/spbooks-PHPPRO1-ae9bb56/chapter_03/wsdl.wsdl");
 
$server->setClass('ServiceFunctions');
$server->handle();

ServiceFunctions.php


<?php
class ServiceFunctions 
{
  public static function getDisplayName($first_name, $last_name) {
    $name = '';
    $name .= strtoupper(substr($first_name, 0, 1));
    $name .= ' ' . strtoupper($last_name) . ' ' . ucfirst($last_name). " static" ;//strtoupper
    return $name;
  }

  public static function countWords($paragraph) {
    $words = preg_split('/[. ,!?;]+/',$paragraph);
    return count($words);
  }
}
?>

see above… example using class not function like your example…

It took me a while to figure it out, but I got it working for me.

First, do you really have this folder structure?
[noparse]http://localhost/VT/Learnable/OOP-PHP/spbooks-PHPPRO1-ae9bb56/spbooks-PHPPRO1-ae9bb56/chapter_03/soap-server-WSDL.php[/noparse]

I wasn’t about to replicate that and simply put the 4 files in
[noparse]http://localhost/soap/[/noparse]

The problem is with the WSDL file. It doesn’t specify “type” for the “Response” returns, so nothing gets returned.
Try changing this section by adding the “parts”

.....
  <message name="countWords">
<!--
	<part name="paragraph" type="xsd:anyType"></part>
-->
	<part name="paragraph" type="xsd:string"></part>
  </message>
  <message name="countWordsResponse">
  <part name='Result' type='xsd:int'/> </message>
  <message name="getDisplayName">
<!--
    <part name="first_name" type="xsd:anyType"></part>
	<part name="last_name" type="xsd:anyType"></part>
-->
	<part name="first_name" type="xsd:string"></part>
	<part name="last_name" type="xsd:string"></part>
  </message>
  <message name="getDisplayNameResponse">
  <part name='Result' type='xsd:string'/> </message>
.....

“…can you provide wsdl that returns …”
I say this in first post… well this you say is to declare string for input… isn’t output that counts?
http://localhost/VT/Learnable/OOP-PHP/spbooks-PHPPRO1-ae9bb56/spbooks-PHPPRO1-ae9bb56/chapter_03/soap-client-live-wsdl-xml.php

prior
array(2) { [0]=> string(35) “void countWords(anyType $paragraph)” [1]=> string(60) “void getDisplayName(anyType $first_name, anyType $last_name)” }
after
array(2) { [0]=> string(34) “void countWords(string $paragraph)” [1]=> string(58) “void getDisplayName(string $first_name, string $last_name)” }

does not proceed…well?

Your __getFunctions() is still showing “void”
Did you only change the “input” “parts” and not add the new “response” “parts”?

wsdl syntax - i do not know well… i tried used code example came with sitepoint/learnable book of Lorna… can you tell if code example is correct as of wsdl file?
the exact file is this - is it for functions give output or only void??? can you correct ?


<?xml version='1.0' encoding='UTF-8'?>
<definitions name="SimpleWSDL" targetNamespace="urn:SimpleWSDL"
xmlns:typens="urn:SimpleWSDL" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://schemas.xmlsoap.org/wsdl/">
  <message name="countWords"><part name="paragraph"
type="xsd:anyType"></part></message>
  <message name="countWordsResponse"></message>
  <message name="getDisplayName"><part name="first_name"
type="xsd:anyType"></part><part name="last_name"
type="xsd:anyType"></part></message>
  <message name="getDisplayNameResponse"></message>
  <portType name="ServiceFunctionsPortType">
    <operation name="countWords"><input
message="typens:countWords"></input><output
message="typens:countWordsResponse"></output></operation>
    <operation name="getDisplayName"><input
message="typens:getDisplayName"></input><output
message="typens:getDisplayNameResponse"></output></operation>
  </portType>
  <binding name="ServiceFunctionsBinding"
type="typens:ServiceFunctionsPortType"><soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"></soap:binding>
    <operation name="countWords">
      <soap:operation soapAction="urn:ServiceFunctionsAction"></soap:operation>
      <input><soap:body namespace="urn:SimpleWSDL" use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"></soap:body></input>
      <output><soap:body namespace="urn:SimpleWSDL" use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"></soap:body></output>
    </operation>
    <operation name="getDisplayName">
      <soap:operation soapAction="urn:ServiceFunctionsAction"></soap:operation>
      <input><soap:body namespace="urn:SimpleWSDL" use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"></soap:body></input>
      <output><soap:body namespace="urn:SimpleWSDL" use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"></soap:body></output>
    </operation>
  </binding>
  <service name="SimpleWSDLService">
    <port name="ServiceFunctionsPort"
binding="typens:ServiceFunctionsBinding"><soap:address location="http://localhost/soap-
server.php"></soap:address></port>
  </service>
</definitions>


can you fix wsdl so as (for functions that) return values? where are “response” “parts”? you know andy site you put functions or class/methods and get wsdl?

I added output


  <message name="countWords"><part name="paragraph" 
type="xsd:string"></part></message>
  <message name="countWordsResponse"><part name="words" type="xsd:integer"></part></message>
  <message name="getDisplayName"><part name="first_name" 
type="xsd:string"></part><part name="last_name" 
type="xsd:string"></part></message>
  <message name="getDisplayNameResponse"><part name="name" type="xsd:string"></part></message>

now getting
array(2) { [0]=> string(37) “integer countWords(string $paragraph)” [1]=> string(60) “string getDisplayName(string $first_name, string $last_name)” }

but
this never runs: // white space rather the result… well?
echo $client->getDisplayName(‘Joe’, ‘Bloggs’);

well is it correct the wsdl or can you fix wsdl so as (for functions that) return values?

Sorry to be gone so long.

You put “integer” instead of int

With this for the “client” (*note location and name differences for me, don’t just copy/paste)

<?php
try
{
ini_set('soap.wsdl_cache_enabled', 0);
$client = new SoapClient('http://localhost/soap/lse123.wsdl');
var_dump($client->__getFunctions());
echo "<hr/>";
echo $client->getDisplayName('Joe', 'Bloggs');
echo "<hr/>";
echo $client->countWords('Joe Bloggs was here having a great time');
echo "<hr/>";
}
catch (SoapFault $fault)
{
    echo "SOAP Fault: faultcode: " . $fault->faultcode . " faultstring: " . $fault->faultstring;
}
?>

I see

I retried with “int” but same thing. no work functions…

I get only

array(2) { [0]=> string(33) "int countWords(string $paragraph)" [1]=> string(60) "string getDisplayName(string $first_name, string $last_name)" } 

and 3 <hr/> without anything between without exception texts…