-1

I need to contact a WebService:

this WS accepts only POST.

For authenticating I have to send some JSON in the BODY of request

while in the HEADER I have to send the WS method I want to call.

This is a valid request sent using CLI (WS answers correctly)

curl -X POST -k -H 'Operation: TPLGetCardData' -H 'card_num: 123456789' -i 'https://example.com/ws.aspx' --data '{
                "auth": [
                    {
                        "Timestamp": 1669910083,
                        "SenderIdentifier": "XXX-XXX-XXXX",
                        "ConnectionKey": "XXXX"
                    }
                ]
            }'

This is the PHP code I've written, but I receive an error from the WS

   
    $data = '{
                "auth": [
                    {
                        "Timestamp": 1669910083,
                        "SenderIdentifier": "XXX-XXX-XXXX",
                        "ConnectionKey": "XXXX"
                    }
                ]
            }';

    $cURLConnection = curl_init();

    curl_setopt($cURLConnection, CURLOPT_URL, 'https://example.com/ws.aspx');
    curl_setopt($cURLConnection, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($cURLConnection, CURLOPT_POST, true);
    curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, http_build_query($data));
    //curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $data);
    curl_setopt($cURLConnection, CURLOPT_HTTPHEADER, array('Operation: TPLGetCardData', 'card_num: 123456789'));
    //curl_setopt($cURLConnection, CURLOPT_VERBOSE , true);
    $result = curl_exec($cURLConnection);

    curl_close($cURLConnection);

    $jsonArrayResponse - json_decode($result);  
    print_r('RESULT is <pre>'.$result.'</pre>'); 

If I send the request with

curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $data)

the error is "no credentials"

if I send the request with

curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, http_build_query($data));

the error is "wrong credentials"

I don't understand which is the difference between what I send with curl CLI command and what I send with PHP.

If someone could help me, it will be really apreciated

:::EDIT::: Sorry, it came out that the problem was on the WS side, my request was OK...2 days lost in finding a non existing problem.

Bux
  • 57
  • 2
  • 11
  • 1
    Sorry I can't help much but you could try to send both requests to a echo server or something, checking the request headers and body and comparing... Maybe we are missing something? – Salketer Dec 02 '22 at 11:58
  • Why not using Soap request directly from php [SoapClient](https://www.php.net/manual/en/book.soap.php#96033) – Pascal Tovohery Dec 02 '22 at 12:19
  • I think you need to encode the data before curl. Let me try provide answer – Ibrahim Hammed Dec 02 '22 at 14:47
  • @PascalTovohery the Webservice isn't SOAP or WSDL and, for what I know, SoapClient works only with these kind of Servers – Bux Dec 02 '22 at 14:48
  • 1
    A "POST param" _is_ part of the body; and `CURLOPT_POSTFIELDS` _is_ what you use to set the request body. https://stackoverflow.com/questions/871431/raw-post-using-curl-in-php – CBroe Dec 02 '22 at 14:49

2 Answers2

0

Try this by encoding the data before passing it to curl

$data = json_encode(array(
    "Timestamp" => "1669910083",
    "SenderIdentifier" => "XXX-XXX-XXXX",
    "ConnectionKey" => "XXXX"
    "ShortCode" => "600981"
));

curl_setopt($cURLConnection, CURLOPT_POSTFIELDS, $data);
Ibrahim Hammed
  • 833
  • 1
  • 8
  • 17
0

I apologize to everyone who helped, but it came out that the problem was on the WS side, my request was OK...

2 days lost in finding a non existing problem. Sorry

Bux
  • 57
  • 2
  • 11