PHP Bad Request in Curl on SOAP interface of http://www.cyberlogic.gr/webservices/PlaceSearch

前端 未结 5 1610
伪装坚强ぢ
伪装坚强ぢ 2021-01-26 03:10

I am trying to call the url using curl in php. I get a BAD REQUEST error . if someone can help me,I do not get what the problem is

Their \"recipe\" is as follows: http:/

5条回答
  •  臣服心动
    2021-01-26 03:48

    The SOAP interface of the www.cyberlogic.gr webservice for the PlaceSearch requires you to pass the xml parameter properly encoded - otherwise you'll get a 400 Bad Request response.

    This is normally done best by using the XML library of your choice, here I do that with SimpleXML which is pretty handy for your use-case:

    // create the request XML
    $request            = new SimpleXMLElement('');
    $request->Username  = 'SERUNCD';
    $request->Password  = 'TA78UNC';
    $request->PlaceType = 'Cities';
    $request->Language  = 'en';
    

    Next to creating correct XML for the parameter to be passed, the parameter itself needs to be properly encoded as well. As this involves some values configured, first of all define the data needed for the whole operation:

    $soapUrl   = "http://wl.filos.com.gr/services/WebService.asmx";
    $namespace = "http://www.cyberlogic.gr/webservices/";
    $action    = "PlaceSearch";
    

    and then use the $request object just created to turn it into the needed SOAP parameter. It is important here, that the XML encoding is applied correctly as well. Again, this should be done with the XML library of your choice, here I do that with DOMDocument:

    // create the soap variable
    $var = new DOMDocument();
    $var->appendChild($var->createElementNS($namespace, 'xml'));
    $var->documentElement->nodeValue = $request->asXML();
    $soapVar = new SoapVar($var->saveXML($var->documentElement), XSD_ANYXML);
    

    Note that the XML is properly assigned as XML to the nodeValue. This is important to create a valid request.

    Now as $soapVar has been created, all it needs it so setup the SoapClient:

    // create soap client in non-WSDL mode
    $client = new SoapClient(null, array(
        'location' => $soapUrl,
        'uri'      => $namespace
    ));
    

    And then do the request:

    // place soap call
    $result = $client->__soapCall(
        $action, array($soapVar), array('soapaction' => "$namespace$action")
    );
    

    And that's it already. Note that the API you have has some XML problems, namely it makes use of an XML element with the tag-name "xml" which is reserved and not allowed. That's also a sign that you should take extra care when you need to trouble-shoot your webservice requests. As curl can only give you a 400 Bar request, SoapClient does actually return much better error messages which help a lot to get things started.

    What's left is the processing of the return value, here exemplary:

    // process result
    foreach ($result->PlaceSearch->Response->Cities->City as $i => $City) {
        printf("#%03d: %s\n", $i, $City->CityName);
    }
    

    Which outputs:

    #000: Achladies
    #001: Afitos
    #002: Agia Paraskevi
    ...
    #142: Volos town
    #143: Vourvourou
    #144: Vrachos- Loutsa
    

    The code-example in full incl. error handling for the soap request:

    /**
     * Example for cyberlogic.gr SOAP webservice
     */
    $soapUrl   = "http://wl.filos.com.gr/services/WebService.asmx";
    $namespace = "http://www.cyberlogic.gr/webservices/";
    $action    = "PlaceSearch";
    
    // create the request XML
    $request            = new SimpleXMLElement('');
    $request->Username  = 'SERUNCD';
    $request->Password  = 'TA78UNC';
    $request->PlaceType = 'Cities';
    $request->Language  = 'en';
    
    // create the soap variable
    $var = new DOMDocument();
    $var->appendChild($var->createElementNS($namespace, 'xml'));
    $var->documentElement->nodeValue = $request->asXML();
    $soapVar = new SoapVar($var->saveXML($var->documentElement), XSD_ANYXML);
    
    // create soap client in non-WSDL mode
    $client = new SoapClient(null, array(
        'location' => $soapUrl,
        'uri'      => $namespace
    ));
    
    try {
        // place soap call
        $result = $client->__soapCall(
            $action, array($soapVar), array('soapaction' => "$namespace$action")
        );
    
        // process result
        foreach ($result->PlaceSearch->Response->Cities->City as $i => $City) {
            printf("#%03d: %s\n", $i, $City->CityName);
        }
    } catch (Exception $e) {
        echo $e->getMessage(), "\n";
        echo $client->__getLastRequest(); // set 'trace' => true SoapClient option
    }
    

提交回复
热议问题