skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. 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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search