这两天,看到上海的情况,心里有些难受。国际化都市的人们,在21世纪20年代,还会饥肠辘辘。
今天我们学习一下PHP如何调用SAP系统发布的WEB SERVICE服务。
SAP作为服务提供者PROVIDER,PHP作为服务消费者CONSUMER。
详细情况为:
- SAP端设计
输入参数:ZINPUT ,类型STRING
输入参数:ZTYPE ,类型STRING
内表参数:RET_DATA ,参照类型为ZMSG,类型ZMSG的字段为MSG,CHAR,长度255.SAP接口函数代码为:
FUNCTION YFM_CHECK.
*”———————————————————————-
*”*”Local Interface:
*” IMPORTING
*” VALUE(ZINPUT) TYPE STRING OPTIONAL
*” VALUE(ZTYPE) TYPE STRING OPTIONAL
*” TABLES
*” RET_DATA STRUCTURE ZMSG OPTIONAL
*”———————————————————————-
* MESSAGE x398(00).
* & & & &CONCATENATE ‘第1行您输入了:ZINPUT=’ ZINPUT ‘,ZTYPE=’ ZTYPE INTO RET_DATA RESPECTING BLANKS.
APPEND RET_DATA.CONCATENATE ‘第2行today:’ sy–datum ‘日-Time:’ sy–uzeit ‘秒’ INTO RET_DATA RESPECTING BLANKS.
APPEND RET_DATA.ENDFUNCTION.
将RFC函数YFM_CHECK发布成WEB SERVICE服务后,名字都用YFM_CHECK。在此不做过多概述。生成了WSDL文件。
将<wsp:UsingPolicy wsdl:required=”true“/>修改成<wsp:UsingPolicy wsdl:required=”false“/> - PHP端代码
写入PHP代码如下:<?php
echo “<pre>”;
header(“content-type:text/html;charset=utf-8”);
try {$client = new SoapClient(“http://www.destinationcom/PHP2SAP.wsdl“,
array(
“stream_context” => stream_context_create(
array(
‘ssl’ => array(
‘verify_peer’ => false,
‘verify_peer_name’ => false,
)
)
)
)
);
print_r(“__getFunctions========================================================\n”);
print_r($client->__getFunctions());
print_r(“__getTypes========================================================\n”);
print_r($client->__getTypes());class cls_table_RET_DATA {
public $ZMSG;
}$INPUT_TABLE_RET_DATA = new cls_table_RET_DATA ();
$INPUT_TABLE_RET_DATA->ZMSG= “100”;$parm = array( ‘ZINPUT’ => “aaa”,
‘ZTYPE’ => “bbb”,
‘RET_DATA’ => $INPUT_TABLE_RET_DATA );$result = $client->YFM_CHECK($parm);
print_r(“result========================================================\n”);
print_r($result);
print_r(“result =》RET_DATA========================================================\n”);
print_r($result->RET_DATA);print_r(“var_dump result RET_DATA======================================================== \n”);
var_dump($result->RET_DATA);print_r(“FOR======================================================== \n”);
//value与key都可输出
foreach($result->RET_DATA as $key=>$val)
{
print_r($key) ;
print_r($val) ;//$key.”=>”.$val;
$lt = $val;print_r(“FOR22 in val1======================================================== \n”);
foreach($lt as $key2=>$val2)
{
print_r($key2) ;
print_r($val2) ;
}}
print_r(“json_encode======================================================== \n”);
//将stdclass object的$result转换为array
$result = json_encode(get_object_vars($result));
var_dump($result);} catch (SOAPFault $e) {
print_r(“ERROR。。”);
print $e;
}echo “</pre>”;
?> - 运行结果
可以看到运行后的输出为__getFunctions======================================================== Array ( [0] => YFM_CHECKResponse YFM_CHECK(YFM_CHECK $parameters) [1] => YFM_CHECKResponse YFM_CHECK(YFM_CHECK $parameters) ) __getTypes======================================================== Array ( [0] => string char255 [1] => string string [2] => struct ZMSG { char255 MSG; } [3] => struct TABLE_OF_ZMSG { ZMSG item; } [4] => struct YFM_CHECK { TABLE_OF_ZMSG RET_DATA; string ZINPUT; string ZTYPE; } [5] => struct YFM_CHECKResponse { TABLE_OF_ZMSG RET_DATA; } ) result======================================================== stdClass Object ( [RET_DATA] => stdClass Object ( [item] => Array ( [0] => stdClass Object ( [MSG] => 第1行您输入了:ZINPUT=aaa,ZTYPE=bbb ) [1] => stdClass Object ( [MSG] => 第2行today:20220408日-Time:172237秒 ) ) ) ) result =》RET_DATA======================================================== stdClass Object ( [item] => Array ( [0] => stdClass Object ( [MSG] => 第1行您输入了:ZINPUT=aaa,ZTYPE=bbb ) [1] => stdClass Object ( [MSG] => 第2行today:20220408日-Time:172237秒 ) ) ) var_dump result RET_DATA========================================================
C:\wamp64\www\test3.php:51: object(stdClass)[4] public 'item' => array (size=2) 0 => object(stdClass)[5] public 'MSG' => string '第1行您输入了:ZINPUT=aaa,ZTYPE=bbb' (length=42) 1 => object(stdClass)[6] public 'MSG' => string '第2行today:20220408日-Time:172237秒' (length=41)
FOR======================================================== itemArray ( [0] => stdClass Object ( [MSG] => 第1行您输入了:ZINPUT=aaa,ZTYPE=bbb ) [1] => stdClass Object ( [MSG] => 第2行today:20220408日-Time:172237秒 ) ) FOR22 in val1======================================================== 0stdClass Object ( [MSG] => 第1行您输入了:ZINPUT=aaa,ZTYPE=bbb ) 1stdClass Object ( [MSG] => 第2行today:20220408日-Time:172237秒 ) json_encode========================================================
C:\wamp64\www\test3.php:76:string '{"RET_DATA":{"item":[{"MSG":"\u7b2c1\u884c\u60a8\u8f93\u5165\u4e86\uff1aZINPUT=aaa,ZTYPE=bbb"},{"MSG":"\u7b2c2\u884ctoday\uff1a20220408\u65e5-Time:172237\u79d2"}]}}' (length=164)
- 总结注意事项
代码要仔细,记得修改WSDL的policy,否则会报错。作者HOT583.com曾经也是SAP ABAP beginners ,到处去找Tutorial for Constant。
需要自学很久,没人给咱免费培训。上面测试代码DEMO例子是成功的代码。希望您喜欢~
PHP是全世界最好的语音~
本文链接 : http://www.hot583.com/2022/04/08/php-call-sap-rfc-fms-web-service/
本文为作者原创,如果这篇文章对您有用,欢迎转载,
转载请务必注明来源《今日热点在看》http://www.hot583.com/
我的微信公众号【 hot583 】 或者在这个链接联系我
微信原文《全文完》