简体   繁体   中英

PHP SOAP Web Service

I am trying to create a simple PHP webservice as I am a newbie in this track. I decided to develop it using SOAP. I am using WAMP as a server and the problem is that I am unable to run the scripts nor get the WSDL file.

Here's server.php's code:

<?php
//call library 
require_once ('lib/nusoap.php'); 
//using soap_server to create server object 
$server = new soap_server; 

//register a function that works on server 
$server->register('get_message'); 

// create the function 
function get_message($your_name) 
{ 
if(!$your_name){ 
return new soap_fault('Client','','Put Your Name!'); 
} 
$result = "Hello World ".$your_name .". Thanks for Your First Web Service Using PHP with SOAP"; 
return $result; 
} 
// create HTTP listener 
$server->service($HTTP_RAW_POST_DATA); 
exit(); 
?>  

and here's a screenshot of the run:

运行server.php

Here's client.php's code:

<?php 
require_once ('lib/nusoap.php'); 
//Give it value at parameter 
$param = array( 'your_name' => 'Omar'); 
//Create object that referer a web services 
$client = new soapclient('http://localhost/WebServiceSOAP/server.php'); 
//Call a function at server and send parameters too 
$response = $client->call('get_message',$param); 
//Process result 
if($client->fault) 
{ 
echo "FAULT: <p>Code: (".$client->faultcode."</p>"; 
echo "String: ".$client->faultstring; 
} 
else 
{ 
echo $response; 
} 
?> 

and here's a screenshot of the run:

running client.php 在此输入图像描述

plus this error keeps bugging me:

Undefined variable: HTTP_RAW_POST_DATA

can u try this below code

$client = new soapclient('http://localhost/WebServiceSOAP/server.php'); 

to

$client = new SoapClient(
    null,
    array(
        'location' => 'ADD YOUR LOCATION',
        'uri' => 'ADD YOUR WSDL FILE ',
        'trace' => 1,
        'use' => SOAP_LITERAL,
    )
);

You're trying to work with undefined variable $HTTP_RAW_POST_DATA . In PHP7 this hook is removed. You can read here

Instead of that I propose to do it like this:

$server->service(file_get_contents("php://input"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM