Skip to Content

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 🙂 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 🙂 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

image

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.

image

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>

To report this post you need to login first.

13 Comments

You must be Logged on to comment or reply to a post.

    1. Rashid Javed Post author
      Thanks a lot Renjith. I do like cricket. And it seems like fellow SDNers are keen to improve and implement this idea in different SAP environments just as Umair did in portal
      (0) 
  1. Umair Salam
    Very cool idea Rashid. I have been wondering about how to keep up with the scores of the matches in this world cup and this is the perfect solution! Since I work in the Portal more than in R/3, I took the RSS feed URL you posted and created an XML iView. I also placed a weather iView I already have in the Portal (adding Kingston, Jamaica’s weather option) beside the scores to keep tabs on if things will get wet 😉
    here’s a screenshot of the page in the Portal:

    http://www.salambazar.com/umairsImages/scores.jpg

    Thanks,
    Umair
    Good luck to Pakistan 😉

    (0) 
    1. Rashid Javed Post author
      Well that was very clever Umair 🙂 In fact you have given me hope that cricket fans from other SAP technologies would participate. May be we will see some one using BW to store all the live scores for later analysis or publishing the score as an internal web service across corporate intranet 🙂 Where are all those cricket fans when you need them?
      (0) 

Leave a Reply