PHP WEBSERVICE nowsdl mode (no arguments, string arguments, array parameters, object parameters) IOS parameter is XML

Source: Internet
Author: User

Directory structure:

Site root directory

--------Soap

----------------server.php client.php

server.php:

<?php

Header (' Content-type:text/html;charset=utf-8 ');

$SoapArrtitue = Array (' Location ' = ' http://www.z.cn/soap/server.php ',

' uri ' = ' http://www.z.cn/soap ',

' Soap_version ' = soap_1_2,

' Encoding ' = ' UTF-8 ');

$SoapServerHandle = new SoapServer (null, $SoapArrtitue);

$SoapServerHandle->addfunction (Soap_functions_all); Allow clients to invoke all methods

$SoapServerHandle->handle ();

function HELLO ($ARGUMENT)

{

if (false = = Isset ($ARGUMENT)) return ' (null) HELLO NULL ';

else if (true = = Is_string ($ARGUMENT)) return ' (string) HELLO '. $ARGUMENT;

else if (true = = Is_array ($ARGUMENT))

{////array using $object[' NAME '] $OBJECT [' Age ']

$GREAT = ' HELLO '. $ARGUMENT [' NAME ']. ', Y 2 '. $ARGUMENT [' age ']. ' Old ';

Return Array (' TYPE ' = ' array ', ' great ' = = $GREAT, ' WINNER ' = ' WINNER ');

}

else if (true = = Is_object ($ARGUMENT))

{///objects use $object->name $OBJECT->age

$GREAT = ' HELLO '. $ARGUMENT->name. ', Y 2 '. $ARGUMENT->age. ' Old ';

$RETURN->type = ' OBJECT ';

$RETURN->great = $GREAT;

$RETURN->winner = ' WINNER ';

return $RETURN;

}

}

?>



client.php:

<?php

Header (' Content-type:text/html;charset=utf-8 ');

$SoapArrtitue = Array (' Location ' = ' http://www.z.cn/soap/server.php ',

' uri ' = ' http://www.z.cn/soap ',

' Soap_version ' = soap_1_2,

' Encoding ' = ' UTF-8 ');

$SoapClientHandle = new SoapClient (null, $SoapArrtitue);

Var_dump ($SoapClientHandle->__gettypes ());

Try

{

echo "HELLO:";

$ResponseHELLONULL = $SoapClientHandle->hello ();

Echo $ResponseHELLONULL;

Echo ' </br> ';

echo "hellostring:";

$ResponseHELLOString = $SoapClientHandle->hello ("Zhang");

Echo $ResponseHELLOString;

Echo ' </br> ';

echo "Helloarray:";

$ARRAY = ARRAY (' NAME ' = ' ZHANG ', ' age ' = ' 18 ');

$ResponseHELLOArray = $SoapClientHandle->hello ($ARRAY);

Print_r ($ResponseHELLOArray);

Echo ' ('. $ResponseHELLOArray [' TYPE ']. " &NBSP|&NBSP ". $ResponseHELLOArray [' Great '];

echo "&nbsp|&nbsp". $ResponseHELLOArray [' WINNER ']. ') </br> ';

echo "Helloobject:";

$OBJECT->name = ' ZHANG ';

$OBJECT->age = ' 20 ';

$ResponseHELLOObject = $SoapClientHandle->hello ($OBJECT);

Print_r ($ResponseHELLOObject);

Echo ' ('. $ResponseHELLOObject->type. &nbsp|&nbsp ". $ResponseHELLOObject->great;

echo "&nbsp|&nbsp". $ResponseHELLOObject->winner. ') </br> ';

}

catch (SoapFault $SoapFaultHandle)

{

Var_dump ($SoapFaultHandle);

Echo ' </br> ';

Echo ' </br> ';

Echo ' </br> ';

Echo ' </br> ';

Print ($SoapClientHandle->__getlastrequest ());

}

?>

iOS code:

NSString *nsstringargumentstring = @ "STRING";

NSString *nsstringargumentobject = @ "<NAME>ZHANG</NAME><AGE>20</AGE>";

NSString *soapmsg = [NSString stringWithFormat:

@ "<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>"

"<soap12:envelope"

"Xmlns:xsi=\" Http://www.w3.org/2001/xmlschema-instance\ ""

"Xmlns:xsd=\" Http://www.w3.org/2001/xmlschema\ ""

"Xmlns:soap12=\" http://www.w3.org/2003/05/soap-envelope\ ">"

"<soap12:Body>"

"

"<ARGUMENT>%@</ARGUMENT>"

"</HELLO>"

"</soap12:Body>"

"</soap12:Envelope>",

Nsstringargumentobject];

Print out this XML string

NSLog (@ "%@\n\n", soapmsg);

Create the URL, which is the second line in the previous request message message, plus the first line URL field

Nsurl *url = [Nsurl urlwithstring: @ "http://www.z.cn/soap/server.php"];

Create a request based on the URL above

Nsmutableurlrequest *req = [Nsmutableurlrequest Requestwithurl:url];

NSString *msglength = [NSString stringwithformat:@ "%d", [soapmsg length]];

Add details of the request, corresponding to the fields in the first half of the request message

[Req addvalue:@ "application/soap+xml; charset=utf-8" forhttpheaderfield:@ "Content-type"];

[Req addvalue:msglength forhttpheaderfield:@ "Content-length"];

Set the request line method to post, corresponding to the first line of the request message

[Req sethttpmethod:@ "POST"];

Add a SOAP message to the request

[Req sethttpbody: [soapmsg datausingencoding:nsutf8stringencoding]];

Create a connection

conn = [[Nsurlconnection alloc] Initwithrequest:req delegate:self];

IF (conn) {

WebData = [Nsmutabledata data];

}

Code can not understand, see HTTP://HI.BAIDU.COM/ZORRU/ITEM/54C225FF3B28C50AC7DC45F1 article, the code is from this article modified from, changed the transfer parameters.

This way to pass the picture and text complex is no problem, a lot more fields, text with text mode, picture binary numbers with Base64 encoding conversion into characters, all OK ... Staging a picture is fine, because the XML field can have a lot of information attached.

PHP WEBSERVICE nowsdl mode (no arguments, string arguments, array parameters, object parameters) IOS parameter is XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.