二维码

[Rest API] SAP中用json数据格式调用http接口发送短信邮件案例

Twilight发表于 2017-04-06 09:29zhongguomao 最后回复于 2017-08-12 13:52 [复制链接] 5364 3

在系统 VL02N 交货过账后触发发送邮件、短信的功能,短信邮件接口是经过第三方系统封装,然后开发出的API接口。

请求地址: http://180.167.01.01/ns/sends
请求方式: POST
参数格式: JSON
请求参数:
  1. {
  2.     "ncode": "sapOrderSend",
  3.     "source": "订单号",
  4.     "contacts": [
  5.         {
  6.             "code": "订货人邮箱地址",
  7.             "type": "1"
  8.         },
  9.         {
  10.             "code": "订货人手机号",
  11.             "type": "2"
  12.         }
  13.     ],
  14.     "params": {
  15.         "contactName": "订货人姓名",
  16.         "contactCode": "订货人编号",
  17.         "code": "订单号",
  18.         "createDate": "订单创建时间",
  19.         "storageName": "发货仓库名称",
  20.         "shipTypeName": "发货方式名称"
  21.     }
  22. }
复制代码


RFC接口代码:
  1. FUNCTION zot_do_send_message.
  2. *"----------------------------------------------------------------------
  3. *"*"本地接口:
  4. *"  IMPORTING
  5. *"     VALUE(I_VBELN) TYPE  VBELN_VL OPTIONAL
  6. *"  EXCEPTIONS
  7. *"      URL_ERROR
  8. *"----------------------------------------------------------------------
  9.   DATA: lr_http_client TYPE REF TO if_http_client .
  10.   DATA: lv_url TYPE string,
  11.         lv_result TYPE string,
  12.         lv_post_string TYPE string,
  13.         lv_len TYPE i.

  14.   TYPES:BEGIN OF ty_contacts,
  15.           code TYPE string,
  16.           type TYPE char1,
  17.        END OF ty_contacts.
  18.   TYPES:BEGIN OF ty_params,
  19.           contactname  TYPE string,
  20.           contactcode  TYPE string,
  21.           code         TYPE string,
  22.           createdate   TYPE string,
  23.           storagename  TYPE string,
  24.           shiptypename TYPE string,
  25.       END OF ty_params.
  26.   DATA:lt_contacts TYPE TABLE OF ty_contacts.
  27.   TYPES:BEGIN OF ty_message,
  28.           ncode TYPE string,
  29.           source TYPE string,
  30.           contacts LIKE lt_contacts,
  31.           params TYPE ty_params,
  32.         END OF ty_message.
  33.   DATA:ls_message  TYPE ty_message.
  34.   DATA:ls_contacts TYPE ty_contacts.
  35.   DATA:BEGIN OF lt_vbap OCCURS 0,
  36.         vbeln TYPE vbap-vbeln,
  37.         posnr TYPE vbap-posnr,
  38.         ktgrm TYPE vbap-ktgrm,
  39.       END OF lt_vbap.

  40.   DATA:lv_vbeln_so   TYPE vbak-vbeln,
  41.        lv_tel_number TYPE adr2-tel_number,
  42.        lv_smtp_addr  TYPE adr6-smtp_addr,
  43.        lv_kunnr      TYPE kna1-kunnr,
  44.        lv_name       TYPE kna1-name1,
  45.        lv_erdat TYPE vbak-erdat,
  46.        lv_bname TYPE vbak-bname,
  47.        lv_bezei TYPE t173t-bezei,
  48.        lv_lgobe TYPE t001l-lgobe.

  49.   IF i_vbeln IS INITIAL.
  50.     EXIT.
  51.   ENDIF.

  52.   SELECT SINGLE url INTO lv_url FROM zot_message WHERE fm_name = 'ZOT_DO_SEND_MESSAGE' AND pm1 = 'URL'.
  53.   IF sy-subrc NE 0.
  54.     MESSAGE '请在表ZOT_MESSAGE配置URL参数' TYPE 'E' RAISING url_error.
  55.   ENDIF.

  56.   SELECT vbap~vbeln
  57.          vbap~posnr
  58.          vbap~ktgrm
  59.     INTO TABLE lt_vbap
  60.     FROM lips
  61.     INNER JOIN vbap ON lips~vgbel = vbap~vbeln AND vbap~abgru = ''
  62.     INNER JOIN vbak ON vbap~vbeln = vbak~vbeln AND vbak~vkorg = '3110'  "国内发短信
  63.     WHERE lips~vbeln = i_vbeln.
  64.   DELETE lt_vbap WHERE ktgrm = '02'.  "技术服务
  65.   IF lt_vbap[] IS INITIAL.
  66.     EXIT.
  67.   ENDIF.

  68.   SELECT SINGLE lips~vgbel adr2~tel_number adr6~smtp_addr kna1~kunnr kna1~name1
  69.   INTO (lv_vbeln_so,lv_tel_number,lv_smtp_addr,lv_kunnr,lv_name)
  70.   FROM lips
  71.   INNER JOIN vbpa ON lips~vgbel = vbpa~vbeln AND vbpa~posnr = space AND vbpa~parvw = 'WE'
  72.   INNER JOIN kna1 ON vbpa~kunnr = kna1~kunnr
  73.   LEFT JOIN adr2 ON kna1~adrnr = adr2~addrnumber AND adr2~persnumber = space AND adr2~r3_user = '3'
  74.   LEFT JOIN adr6 ON kna1~adrnr = adr6~addrnumber AND adr6~persnumber = space
  75.   WHERE lips~vbeln = i_vbeln.

  76.   IF lv_tel_number IS INITIAL AND lv_smtp_addr IS INITIAL.
  77.     EXIT.
  78.   ENDIF.

  79.   SELECT SINGLE
  80.   likp~erdat
  81.   vbak~bname
  82.   t173t~bezei
  83.   t001l~lgobe
  84.   INTO (lv_erdat,lv_bname,lv_bezei,lv_lgobe)
  85.   FROM likp
  86.   INNER JOIN lips ON likp~vbeln = lips~vbeln
  87.   INNER JOIN vbak ON lips~vgbel = vbak~vbeln
  88.   LEFT JOIN t173t ON t173t~spras = sy-langu AND t173t~vsart = likp~vsart
  89.   LEFT JOIN t001l ON lips~werks = t001l~werks AND lips~lgort = t001l~lgort
  90.   WHERE likp~vbeln = i_vbeln.

  91. *-->数据初始化成嵌套内表形式
  92.   ls_message-ncode = 'sapOrderSend'.
  93.   IF lv_bname IS NOT INITIAL.
  94.     ls_message-source = lv_bname.
  95.   ELSE.
  96.     ls_message-source = i_vbeln.
  97.   ENDIF.
  98.   IF lv_smtp_addr IS NOT INITIAL.
  99.     ls_contacts-code = lv_smtp_addr.
  100.     ls_contacts-type = '1'.
  101.     APPEND ls_contacts TO ls_message-contacts.
  102.   ENDIF.
  103.   IF lv_tel_number IS NOT INITIAL.
  104.     ls_contacts-code = lv_tel_number.
  105.     ls_contacts-type = '2'.
  106.     APPEND ls_contacts TO ls_message-contacts.
  107.   ENDIF.

  108.   ls_message-params-contactname = lv_name.
  109.   ls_message-params-contactcode = lv_kunnr.
  110.   IF lv_bname IS NOT INITIAL.
  111.     ls_message-params-code = lv_bname.
  112.   ELSE.
  113.     ls_message-params-code = lv_vbeln_so.
  114.   ENDIF.

  115.   IF lv_erdat IS NOT INITIAL.
  116.     CONCATENATE lv_erdat+0(4) lv_erdat+4(2) lv_erdat+6(2) INTO ls_message-params-createdate SEPARATED BY '-'.
  117.   ENDIF.
  118.   ls_message-params-storagename = lv_lgobe.
  119.   ls_message-params-shiptypename = lv_bezei.

  120. *-->内表数据转-->json格式
  121.   lv_post_string = /ui2/cl_json=>serialize( data = ls_message compress = abap_true pretty_name = abap_true ).

  122.   REPLACE ALL OCCURRENCES OF 'contactname'  IN lv_post_string WITH 'contactName'.
  123.   REPLACE ALL OCCURRENCES OF 'contactcode'  IN lv_post_string WITH 'contactCode'.
  124.   REPLACE ALL OCCURRENCES OF 'createdate'   IN lv_post_string WITH 'createDate'.
  125.   REPLACE ALL OCCURRENCES OF 'storagename'  IN lv_post_string WITH 'storageName'.
  126.   REPLACE ALL OCCURRENCES OF 'shiptypename' IN lv_post_string WITH 'shipTypeName'.

  127. *  lv_len = strlen( lv_post_string ) .
  128.   lv_len = cl_abap_list_utilities=>dynamic_output_length( lv_post_string ).

  129. *send
  130. *  lv_url = 'http://192.168.01.01:8088/ns/sends'.
  131.   CALL METHOD cl_http_client=>create_by_url
  132.     EXPORTING
  133.       url                = lv_url
  134.     IMPORTING
  135.       client             = lr_http_client
  136.     EXCEPTIONS
  137.       argument_not_found = 1
  138.       plugin_not_active  = 2
  139.       internal_error     = 3
  140.       OTHERS             = 4.

  141.   CALL METHOD lr_http_client->request->set_header_field
  142.     EXPORTING
  143.       name  = '~request_method'
  144.       value = 'POST'.

  145.   CALL METHOD lr_http_client->request->set_header_field
  146.     EXPORTING
  147.       name  = '~server_protocol'
  148.       value = 'HTTP/1.1'.

  149.   CALL METHOD lr_http_client->request->set_header_field
  150.     EXPORTING
  151.       name  = 'Content-Type'
  152.       value = 'application/json; charset=utf-8'.
  153.   CALL METHOD lr_http_client->request->set_header_field
  154.     EXPORTING
  155.       name  = 'Accept'
  156.       value = 'application/json'.
  157. *  CALL METHOD lr_http_client->request->set_header_field
  158. *    EXPORTING
  159. *      name  = 'Content-Length'
  160. *      value = len_str.


  161.   CALL METHOD lr_http_client->request->set_cdata
  162.     EXPORTING
  163.       data   = lv_post_string
  164.       offset = 0
  165.       length = lv_len.

  166.   CALL METHOD lr_http_client->send
  167.     EXCEPTIONS
  168.       http_communication_failure = 1
  169.       http_invalid_state         = 2.
  170.   CALL METHOD lr_http_client->receive
  171.     EXCEPTIONS
  172.       http_communication_failure = 1
  173.       http_invalid_state         = 2
  174.       http_processing_failed     = 3.

  175.   lv_result = lr_http_client->response->get_cdata( ).

  176. ENDFUNCTION.
复制代码
回复

使用道具 举报

Venus
谢谢楼主的分享,请问第三方是怎么联系啊,我们公司现在正想用此功能给客户发短息
回复 支持 反对

使用道具 举报

Twilight

RE: SAP中用json数据格式调用http接口发送短信邮件案例

Venus 发表于 2017-8-10 10:13
谢谢楼主的分享,请问第三方是怎么联系啊,我们公司现在正想用此功能给客户发短息

短信平台都不一样,有阿里大鱼短信接口、云片短信平台,或者联通短信接口,每家的API数据通信格式都可能不一样,看他们提供的API文档,然后具体问题具体分析啦
回复 支持 反对

使用道具 举报

zhongguomao
没用过,不过好高大上,先收藏再说。
回复 支持 反对

使用道具 举报

快速回帖

本版积分规则
您需要登录后才可以回帖 登录 | 注册有礼

快速回复 返回顶部 返回列表