Hi
I am quite new to website development and have been using Joomla to build sites. More recently I have been learning php and I am currently trying to build a module in Joomla to access a web service. I am finding it extremely difficult!

I have read articles here about it but still confused!

I am trying to send the following request at the bottom of this message to the web service and need to be able to send a single number (64) to get data returned.

Can any one point me to the code I need to establish the connection and send the request?

Any help you can give me is greatly appreciated.

Kind regards
John

POST /WhyPCServer/ g e t r i d o f t h i $ b1t WhyPCWebService.asmx HTTP/1.1
Host: 84.92.a n d t h i $ b1t127.185
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?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>
    <GetWhyPCAdminSummaryData xmlns="http://whyismypc.com/">
      <AdminisratorID>int</AdminisratorID>
    </GetWhyPCAdminSummaryData>
  </soap12:Body>
</soap12:Envelope>

    You need a SOAP client to facilitate the transaction.

    By the way, my condolences that you have to use SOAP because it's generally a pain in the backside (a bit of a brain twister for a self-proclaimed newbie I'd imagine too!) Not to mention it's so 2005-ish 🙂

    Actually, there should be a lot of examples out there on how to consume a SOAP service with PHP.

      You are so not wrong about brain twisters! Thanks so much for replying, I feel like one of those Chilean miners been stuck in a dark hole for ages and suddenly a chink of light comes through from the outside!

      Taking on board your comments about SOAP maybe we don't have to stick with it. The guy running the server that I'm trying to access has his data on a windows server I believe. What is a better method to use for the web service in your opinion and can you point me towards some documentation?

      Mean time I will start reading through the info you pointed to.

      Look forward to hearing from you.
      Regards
      John

        ...By better I mean one that is not so much of a brain twister to communicate with using php.
        J

          Hi John,

          It's very easy to publish a web service using Visual studio .NET. So unless the guy on the other end is willing to provide a RESTful or XML-RPC service, SOAP is most likely your best bet.

          Typically, when you alter the Web Service URL on an .asmx URL path with .asmx?wsdl, you will get a listing of all the methods and property that the SOAP service provides (WSDL stands for Web Services Description Language.) With the SOAP Client library in PHP, as long as the types declared in the WSDL are simple (arrays, strings, etc.) It's usually quite simple to make calls on them via the SOAP class (much easier than writing all that XML from scratch) provided that you instantiate the SOAP Client with the full path to the wsdl ( for .NET SOAP services typically http://theothersite.tld/webservice.asmx?wsdl ) If at all, you might want to work with him to keep the type definitions simple for the services you need to consume. I'd say, the best examples are probably there in the manual.

          Good luck.

            Hi Bretticus

            Hope you're keeping well.

            Having eventually realised I needed to instal the php SOAP libraries I made a little progress!😕

            So I set the following:

            use_soap_error_handler (true);
            
            $client = new SoapClient("http://84.92.127.185/WhyPCServer/WhyPCWebService.asmx?wsdl");
            $result=($client->GetWhyPCAdminSummaryData());
            
            
            print_r($result);

            which displays

            stdClass Object ( [GetWhyPCAdminSummaryDataResult] => stdClass Object ( [any] => GoodOKPoor£128LikelyUnlikelyPossiblePossible ) )

            But what I do from here I have no idea. I was assuming I could load the result into the joomla xml parser and then retrieve results assigned to variables and display them on my page etc. But when I try to load $result as a string into the parser the parser contains nothing as far as I can tell.

            I thought maybe it's giving me an array so I also tried foreach {} but nothing.

            I guess I must be missing something.

            Any guidance greatly appreciated.

            Best wishes
            John

            this is the xml sent to me by the guy on the other side:

            <?xml version="1.0" encoding="utf-8" ?>
            - <DataTable xmlns="http://whyismypc.com/">
            - <xs:schema id="NewDataSet" xmlns=""
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
            - <xs:element name="NewDataSet" msdata:IsDataSet="true"
            msdata:MainDataTable="Results" msdata:UseCurrentLocale="true">
            - <xs:complexType>
            - <xs:choice minOccurs="0" maxOccurs="unbounded">
            - <xs:element name="Results">
            - <xs:complexType>
            - <xs:sequence>
              <xs:element name="Application_Rating" type="xs:string" minOccurs="0" />
              <xs:element name="Logon_Rating" type="xs:string" minOccurs="0" />
              <xs:element name="User_Rating" type="xs:string" minOccurs="0" />
              <xs:element name="Hidden_Cost" type="xs:string" minOccurs="0" />
              <xs:element name="Processor_Bottleneck" type="xs:string" minOccurs="0" />
              <xs:element name="Memory_Bottleneck" type="xs:string" minOccurs="0" />
              <xs:element name="Disk_Bottleneck" type="xs:string" minOccurs="0" />
              <xs:element name="Other_Bottleneck" type="xs:string" minOccurs="0" />
              </xs:sequence>
              </xs:complexType>
              </xs:element>
              </xs:choice>
              </xs:complexType>
              </xs:element>
              </xs:schema>
            - <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
            xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
            - <DocumentElement xmlns="">
            - <Results diffgr:id="Results1" msdata:rowOrder="0"
            diffgr:hasChanges="inserted">
              <Application_Rating>Good</Application_Rating>
              <Logon_Rating>OK</Logon_Rating>
              <User_Rating>Poor</User_Rating>
              <Hidden_Cost>£128</Hidden_Cost>
              <Processor_Bottleneck>Likely</Processor_Bottleneck>
              <Memory_Bottleneck>Unlikely</Memory_Bottleneck>
              <Disk_Bottleneck>Possible</Disk_Bottleneck>
              <Other_Bottleneck>Possible</Other_Bottleneck>
              </Results>
              </DocumentElement>
              </diffgr:diffgram>
              </DataTable>
            

              p.s. var_dump

              object(stdClass)#136 (1) { ["GetWhyPCAdminSummaryDataResult"]=> object(stdClass)#137 (1) { ["any"]=> string(1603) "GoodOKPoor£128LikelyUnlikelyPossiblePossible" } }

                Your result is an object, once it is returned from the soap client.
                To access the properties, you need to treat it like an object...

                echo $result->GetWhyPCAdminSummaryDataResult->any;
                

                would output

                GoodOKPoor&#163;128LikelyUnlikelyPossiblePossible

                  Thanks Alf, that's pretty much what I have got and what I did but isn't it possible to get XML and parse it?

                  Should I be able to create a table the shows the results and the field names?

                  Like:

                  Application_Rating: Good
                  Logon_Rating: OK

                  etc

                  Regards
                  John

                    You can use

                    SoapClient::__getLastResponse()

                    $xmlResponse = $client->__getLastResponse();
                    

                    this will give you the XML response....
                    remember parsing XML is costly, Letting the SoapClient to the job is the most effecient, resource wise, to do this.

                      Ok Thank you

                      I tried that too but what is returned won't parse.

                      So if we forget XML and work with the object how would I get the results to show as a table including the field names etc?

                      Reagrds
                      John

                        Actually I just tried this in full and it doesn't return anything:

                        use_soap_error_handler (true);
                        
                        $client = new SoapClient("http://84.92.127.185/WhyPCServer/WhyPCWebService.asmx?wsdl");
                        $result=($client->GetWhyPCAdminSummaryData());
                        echo $result->GetWhyPCAdminSummaryDataResult->any;

                        What did I get wrong?

                        Regards
                        John

                          <?php
                          use_soap_error_handler (true);
                          $client = new SoapClient("http://84.92.127.185/WhyPCServer/WhyPCWebService.asmx?wsdl");
                          $result=($client->GetWhyPCAdminSummaryData());
                          
                          //the API appears to be returning an XML string INSIDE the soap object, so we need to load that
                          //create a new DOMDocument to do the work for us
                          $doc = new DOMDocument('1.0', 'utf-8');
                          //Load the xml string into the DOMDocument
                          $doc->loadXML($result->GetWhyPCAdminSummaryDataResult->any);
                          
                          //now we get the "row" elements from the document and load them into a nodelist
                          $rows = $doc->getElementsByTagName("row");
                          //find out how many "row" elements there are
                          $nodeCount = $rows->length;
                          echo ("There are {$nodeCount} records<br />");
                          //create an array to store our data that we get from the records
                          $data = array();
                          //loop through the records, and load up our array
                          for($i=0; $i<$nodeCount; $i++){
                              $item = $rows->item($i); //here we get each "row" element from the nodelist as a DOMElement
                              //sinc all the data in the output appears to be stored in element attributes,
                              //we will use DomElement::getAttribute() to get our data
                              $data[] = array(
                                  'Application_Rating'   => $item->getAttribute('Application_Rating'),
                                  'Logon_Rating'         => $item->getAttribute('Logon_Rating'),
                                  'User_Rating'          => $item->getAttribute('User_Rating'),
                                  'Hidden_Cost'          => $item->getAttribute('Hidden_Cost'),
                                  'Processor_Bottleneck' => $item->getAttribute('Processor_Bottleneck'),
                                  'Memory_Bottleneck'    => $item->getAttribute('Memory_Bottleneck'),
                                  'Disk_Bottleneck'      => $item->getAttribute('Disk_Bottleneck'),
                                  'Other_Bottleneck'     => $item->getAttribute('Other_Bottleneck')
                              );
                          }
                          //Lets see what we got
                          echo '<pre>'.print_r($data, true).'</pre>';
                          ?>
                          
                            jon2396;10968182 wrote:

                            Actually I just tried this in full and it doesn't return anything:

                            use_soap_error_handler (true);
                            
                            $client = new SoapClient("http://84.92.127.185/WhyPCServer/WhyPCWebService.asmx?wsdl");
                            $result=($client->GetWhyPCAdminSummaryData());
                            echo $result->GetWhyPCAdminSummaryDataResult->any;

                            What did I get wrong?

                            Regards
                            John

                            I tried it as well. View source on the output, and you will see that it is an XML string that was returned. My above example works fine

                              It works great! You STAR!
                              Now I have to figure out how to display it in HTML?
                              That's it?
                              Regards
                              John

                              p.s. have to head out now, back later.

                                Glad I could help...

                                Keep in mind that the code in my example is not production level. I am not checking for errors, or soap faults.

                                You should read up on these in the manual and get a handle on them so if you run into a problem with the API you can handle errors gracefully. Right now that code has no safety nets in the case of errors from the API.

                                  OK thanks Alf I will.

                                  Also is it now possible for me to assign a reult to a variable so I can show something like:

                                  Your Application Rating is Good

                                  I thought I would get this from $data['Application_rating'] but I'm don't.

                                  Or is is $item->'Application_rating'

                                  or $item['Application_rating']
                                  ??

                                  Regards
                                  John

                                    I think I got it

                                    $ob =($data[0]['Other_Bottleneck']);

                                    There's hope for me yet?

                                    I was thinking that if the data on the web server was arranged differently this could be a lot simpler, and use less resources as you mentioned? For example the other call AvailableNamedDateRanges can show the same result as you achieved simply like this:

                                    $result=($client->AvailableNamedDateRanges());
                                    $DateRanges = (Array)$result->AvailableNamedDateRangesResult;
                                    
                                    echo '<pre>'.print_r($DateRanges, true).'</pre>'; 
                                    
                                    echo $DateRanges['string'][2];

                                    Would it be a good idea to restructure GetMyPCAdminSummaryData on the server?

                                    Regards
                                    John

                                      5 months later

                                      Luego de un año... Me sirvio de mucha ayuda...

                                      Muchas gracias..

                                      $datos = $wsResponse->metodoResult->any;
                                      $objDom = new DOMDocument('1.0', 'utf-8');
                                      $objDom->loadXML($datos);
                                      $rows = $objDom->getElementsByTagName('AccionID');
                                      $numNodos = $rows->length;

                                      $valores = array();

                                      for($i=0; $i<$numNodos; $i++){
                                      $item = $rows->item($i)->nodeValue;
                                      $valores[] = array(
                                      'AccionID' => $item
                                      );
                                      }

                                        Write a Reply...