shah
shah

Reputation: 27

cURL command to PHP code returns error

I am trying to post data to create a customer using Rotessa API. I am using curl command which I've found in Rotessa API reference. But I am getting errors in return response instead of JSON string. It would be very helpful if I get help on this. Thanks

curl command:

 curl -X POST -H 'Content-Type: application/json' -H "Authorization: Token 
 token=\"<api_key>\"" -d '{"custom_identifier": "test api", "email": 
 "[email protected]", "name": "Mike Smith", "bank_name": "Scotiabank", 
 "transit_number": "11111", "institution_number": "111", "account_number": 
 "11111111", "address": { "address_1": "123 Main Street", "address_2": "Unit 
 4", "city": "Toronto", "province_code": "ON", "postal_code": "M1B 0B7" }}' 
 <rotessa_endpoint>/customers.json  

and converted php code:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.rotessa.com/v1/customers.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"custom_identifier\": \"test api\", 
                                   \"email\": \"[email protected]\", 
                                   \"name\": \"Mike Smith\", 
                                   \"bank_name\": \"Scotiabank\", 
                                   \"transit_number\": \"11111\", 
                                   \"institution_number\": \"111\", 
                                   \"account_number\": \"11111111\", 
                                   \"address\": { \"address_1\": \"123 Main
                                                                   Street\", 
                                                  \"address_2\": \"Unit 4\", 
                                                  \"city\": \"Toronto\", 
                                                  \"province_code\": \"ON\", 
                                                  \"postal_code\": \"M1B 
                                                               0B7\" }}");

    curl_setopt($ch, CURLOPT_POST, 1);

    $headers = array();
    $headers[] = "Content-Type: application/json";
    $headers[] = "Authorization: Token token=\"<api_key>\"";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
    }


    curl_close ($ch);

    echo $result;

it gives the following error:

    {"errors":[{"error_code":"unauthorized","error_message":"Not Found."}]}

To resolve further confusion I used real api_key in my testing. Just didn't post it here.

Upvotes: 0

Views: 233

Answers (2)

Malus Jan
Malus Jan

Reputation: 2138

Normally the APIs need an API key and should be sent by curl.

curl "<rotessa_endpoint>/customers.json" -H "Authorization: Token token=\"<api_key>\""

Instead of <api_key> should enter your API key.

rotessa API KEY

Upvotes: 0

Daniel W.
Daniel W.

Reputation: 32290

Above you have endpoint URL /customers.json, below you have /customers.

Upvotes: 1

Related Questions