首页 > 代码库 > PHP WEBSERVICE NOWSDL模式 (不传参数,字符串参数,数组参数,对象参数) IOS 参数为XML
PHP WEBSERVICE NOWSDL模式 (不传参数,字符串参数,数组参数,对象参数) IOS 参数为XML
目录结构:
网站根目录
-------- 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); /// 允许客户端调用所有的方法
$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))
{ /// 数组使用$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))
{ /// 对象使用$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‘]." | ".$ResponseHELLOArray[‘GREAT‘];
echo " | ".$ResponseHELLOArray[‘WINNER‘].‘)</br>‘;
echo "HELLOObject:";
$OBJECT->NAME = ‘ZHANG‘;
$OBJECT->AGE = ‘20‘;
$ResponseHELLOObject = $SoapClientHandle->HELLO($OBJECT);
print_r($ResponseHELLOObject);
echo ‘(‘.$ResponseHELLOObject->TYPE." | ".$ResponseHELLOObject->GREAT;
echo " | ".$ResponseHELLOObject->WINNER.‘)</br>‘;
}
catch(SoapFault $SoapFaultHandle)
{
var_dump($SoapFaultHandle);
echo ‘</br>‘;
echo ‘</br>‘;
echo ‘</br>‘;
echo ‘</br>‘;
print ($SoapClientHandle->__getLastRequest());
}
?>
IOS代码:
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>"
"<HELLO xmlns=\"http://www.z.cn/\">"
"<ARGUMENT>%@</ARGUMENT>"
"</HELLO>"
"</soap12:Body>"
"</soap12:Envelope>",
NSStringArgumentObject];
// 将这个XML字符串打印出来
NSLog(@"%@\n\n", soapMsg);
// 创建URL,内容是前面的请求报文报文中第二行主机地址加上第一行URL字段
NSURL *url = [NSURL URLWithString: @"http://www.z.cn/soap/server.php"];
// 根据上面的URL创建一个请求
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
// 添加请求的详细信息,与请求报文前半部分的各字段对应
[req addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
// 设置请求行方法为POST,与请求报文第一行对应
[req setHTTPMethod:@"POST"];
// 将SOAP消息加到请求中
[req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
// 创建连接
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
webData = [NSMutableData data];
}
代码看不懂,看http://hi.baidu.com/zorru/item/54c225ff3b28c50ac7dc45f1文章,代码是从这个文章中修改过来的,就修改了传递参数。
这样传递图片和文字综合体就没问题了,多了很多字段,文字用文本模式,图片二进制数字用base64编码转换成字符,一切ok...分段传输图片也没问题了,因为xml的字段可以附加很多信息。
PHP WEBSERVICE NOWSDL模式 (不传参数,字符串参数,数组参数,对象参数) IOS 参数为XML