Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Rashid_Javed
Contributor
0 Kudos

This blog is about using reading RSS feed from within SAP (CL_HTTP_CLIENT) and applying ST (Simple Transformation) to convert it into ABAP data. The example used here is for cricket scores as ICC cricket World Cup 2007 is going on.


Well may be from the heading you got the impression that I am trying to kill too many birds with one stone but the fact is that all the above mentioned things (cricket, cl_http_client and ST) are interlinked quite closely (at least in this blog )


For those who still think cricket is the name of an insect, well you are partially right, but it is also a very popular sport especially in sub continent (Pakistan, India, Sri Lanka, Bangladesh) England, Australia and many other countries. Further details can be found on the official website of International Cricket Council.


Anyways with cricket world cup 2007 around, getting live scores of competing teams was becoming a high priority for its followers. However opening a new browser window and switching between SAP and web browser was the main reason I started thinking of some way to do it inside SAP. Another reason was if you are found staring at browser for extended periods of time casual observer might assume you are not dedicated enough on your work :smile: So in the following example we will try to get the data in ABAP structures so it can be output like a normal SAP report.


Once the requirement was identified, the next step was to get the data from a web site in form of XML. In this example, that website is www.cricinfo.com . This site provides several RSS feeds and live score feed is one of them. So the first step was to identify what url should be used to get the live scores. After checking the relevant portion of website the exact URL for lives scores seems to be as XML Link

</p>

Fetching the data

Now back to SAP, how to fetch this data. In our SAP environment, WAS 6.40 was available so I decided to use Was functionality to work as HTTP client. SAP standard class can be used for this purpose. Since this was my first time to use this functionality, a where used list of this class results in a program RSHTTP01 and it helped in getting the development started. Following steps were performed in code to get the data.




1: Declare a variable (say ‘client’) with type ref to if_http_client

2: Get a handle in ‘client’ by calling create method of cl_http_client. You must provide the host name(in our example it must be ‘www.cricinfo.com’. Other parameters are optional. If you connect to internet through a proxy, you can pass proxy server name/address in parameter ‘proxy_host’ and its port number in parameter ‘proxy_service’.

3: Once ‘client’ is successfully created, the next step is to send the request to http server. However before calling it we must fill the ‘request’ object with relevant data. ‘Request’ is where we specify our requirements to the http server. In this case only two values are adjusted in ‘request’ object. One is the request_method whose value is ‘GET’ and other is the url which in this case will be http://www.cricinfo.com/rss/livescores.xml. Once ‘response’ object is properly filled, we call method ‘send’ of ‘client’.

4: After ‘send’ request, it is time to receive the data.


This is performed by calling a method ‘receive’ of client. If method call ‘receive’ is successful, the http server response is available in a ‘response’ object. From this ‘response’ object, the actual response can be assigned to a string variable.
Here is the abap code for it.




And here is the code for perform send_get_response.

FORM send_get_response CHANGING p_respd TYPE string.

CLEAR p_respd.

IF client IS INITIAL.
CALL METHOD cl_http_client=>

create

      EXPORTING

        host               = phost

  •    SERVICE            =

        proxy_host         = proxyh

        proxy_service      = proxyp

  •    SCHEME             = SCHEMETYPE_HTTP

  •    SSL_ID             =

  •    SAP_USERNAME       =

  •    SAP_CLIENT         =

      IMPORTING

        client             = client

      EXCEPTIONS

        argument_not_found = 1

        plugin_not_active  = 2

        internal_error     = 3

        OTHERS             = 4

            .

    IF sy-subrc <> 0.

      WRITE:/ ' cl_http_client=>create, subrc = ', sy-subrc.

      EXIT.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  •            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

    ELSE.

      buff = 'GET'.

      CALL METHOD client->request->set_header_field

        EXPORTING

          name  = '~request_method'

          value = buff.

      buff = pbuff.

      cl_http_utility=>set_request_uri( request = client->request

                                        uri     = buff ).

      subrc = cl_http_utility=>get_last_error( ).

      IF subrc <> 0.

        WRITE: / 'Wrong URI format'.

        EXIT.

      ENDIF.

    ENDIF.  "subrc for client creation

  ENDIF.

  CALL METHOD client->send

  • EXPORTING

  •    TIMEOUT                    = CO_TIMEOUT_DEFAULT

    EXCEPTIONS

      http_communication_failure = 1

      http_invalid_state         = 2

      http_processing_failed     = 3

      http_invalid_timeout       = 4

      OTHERS                     = 5

          .

  IF sy-subrc <> 0.
CALL METHOD client->get_last_error
IMPORTING
code = subrc
MESSAGE = buff.
WRITE: / 'communication_error( send )',
/ 'code: ', subrc, 'message: ', buff.
EXIT.
ENDIF.

CALL METHOD client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
IF sy-subrc <> 0.
CALL METHOD client->get_last_error
IMPORTING
code = subrc
MESSAGE = buff.
FORMAT COLOR COL_BACKGROUND.
WRITE: / 'communication_error( receive )',
/ 'code: ', subrc, 'message: ', buff.
WRITE: / 'communication_error'.
EXIT.
ENDIF.

p_respd = client->response->get_cdata( ).

ENDFORM. " send_get_response

Well the above four steps would enable us to get the xml file livescores.xml into a string.

Transforming the results

SAP help for simple transformations is very helpful (well few pages are still in german :smile: and the example in tt:loop was a bit confusing but overall very good read). For this particular project, I know all I needed was to sort out

Well if the result of call transformation is successful, the structure ‘outtab’ will be populated by the values from XML file. Now since this data is in ABAP, you are free to simply write it to list output, save in a database table or display in some ALV grid etc. I simply used write statements to make it look like a traditional ABAP report output 🙂



data: wa_tab like itemtab.

format color 4.
write:/ outtab-ctitle,
/ outtab-cdesc,
/ outtab-cpubdate.
format color off.
skip 1.

loop at outtab-ittab into wa_tab.
write:/ wa_tab-title color 1,
/ wa_tab-description,
/ wa_tab-link.
uline.
endloop.

Conclusion

Well we have successfully displayed the live scores. This was done using the simplest possible approach. Class CL_HTTP_CLIENT has more than one create method. ‘Authenticate’ method can be used in case of proxy server to avoid the username/password dialogue box Also CREATE_BY_DESTINATION can be used to create with reference to an RFC destination. Similarly ST has much more tags than we discussed above. Tobias Trapp has a very good series of blogs on XML processing in ABAP . </p>

13 Comments