Issue With CURL JSON POST

superaff1984

Senior Member
Premium Member
Joined
Jun 16, 2010
Messages
991
Reaction score
131
I can't get this to post, I am trying to update a customer record with a new phone number. What is wrong with my code below?


PHP:
// API URL
$url = "https://openapi.api.com/api/v2/open/customers/10005182";

// Create a new cURL Resource
$curl = curl_init($url);

// Setup request to send json via POST
$data = array(
    "Phone" => "7656493111"
);

$data = json_encode(array($data));

// Attach encoded JSON string to the POST fields
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

// Set the content type to application/json
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: ApiKey DEMO', 'Content-Type:application/json'));

// Return response instead of outputting
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_POST, true);

curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");

$resp = curl_exec($curl);

print_r($resp);
 
There's a few noticable things.

1. You are using both CURLOPT_POST and CURLOPT_CUSTOMREQUEST options. Both are not needed.

2. As @eldes pointed out, you're encoding an array within an array to JSON - so you're getting a nested JSON. Most APIs expect a single object and this could be why your request is being rejected by OpenAPI servers

To give you an example, this is your current code

PHP:
$data = array(
    "Phone" => "7656493111"
);

$data = json_encode(array($data));

The above code, will result in the below output

JSON:
[
    {
        "Phone": "7656493111"
    }
]

Now, let's remove the second array

PHP:
$data = array(
    "Phone" => "7656493111"
);

$data = json_encode($data);

Your JSON object produced is now

JSON:
{
    "Phone": "7656493111"
}

3. cURL is good at returning errors.

If an error occurs, first of all curl_exec() will return false, once/if that happens, we can use curl_error() to echo the errors - so you can determine what is happening.

4. Finally, close your cURL sessions man! Come on -

Below is the code with the fixes, see if you're able to get it to work

PHP:
// API URL
$url = "https://openapi.api.com/api/v2/open/customers/10005182";

// Create a new cURL Resource
$curl = curl_init($url);

// Setup request to send json via POST
$data = array(
    "Phone" => "7656493111"
);

// Encode the data as JSON
$data = json_encode($data);

// Attach encoded JSON string to the POST fields
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

// Set the content type to application/json
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: ApiKey DEMO', 'Content-Type:application/json'));

// Return response instead of outputting
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// Make a POST Request
curl_setopt($curl, CURLOPT_POST, true);

// Execute cURL request and capture response
$resp = curl_exec($curl);

// Check for errors in the cURL request
if ($resp === false) {
    echo 'Curl error: ' . curl_error($curl);
} else {
    print_r($resp);
}

// Close cURL session
curl_close($curl);
 
There's a few noticable things.

1. You are using both CURLOPT_POST and CURLOPT_CUSTOMREQUEST options. Both are not needed.

2. As @eldes pointed out, you're encoding an array within an array to JSON - so you're getting a nested JSON. Most APIs expect a single object and this could be why your request is being rejected by OpenAPI servers

To give you an example, this is your current code

PHP:
$data = array(
    "Phone" => "7656493111"
);

$data = json_encode(array($data));

The above code, will result in the below output

JSON:
[
    {
        "Phone": "7656493111"
    }
]

Now, let's remove the second array

PHP:
$data = array(
    "Phone" => "7656493111"
);

$data = json_encode($data);

Your JSON object produced is now

JSON:
{
    "Phone": "7656493111"
}

3. cURL is good at returning errors.

If an error occurs, first of all curl_exec() will return false, once/if that happens, we can use curl_error() to echo the errors - so you can determine what is happening.

4. Finally, close your cURL sessions man! Come on -

Below is the code with the fixes, see if you're able to get it to work

PHP:
// API URL
$url = "https://openapi.api.com/api/v2/open/customers/10005182";

// Create a new cURL Resource
$curl = curl_init($url);

// Setup request to send json via POST
$data = array(
    "Phone" => "7656493111"
);

// Encode the data as JSON
$data = json_encode($data);

// Attach encoded JSON string to the POST fields
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

// Set the content type to application/json
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: ApiKey DEMO', 'Content-Type:application/json'));

// Return response instead of outputting
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// Make a POST Request
curl_setopt($curl, CURLOPT_POST, true);

// Execute cURL request and capture response
$resp = curl_exec($curl);

// Check for errors in the cURL request
if ($resp === false) {
    echo 'Curl error: ' . curl_error($curl);
} else {
    print_r($resp);
}

// Close cURL session
curl_close($curl);

I appreciate it and did everything as mentioned, but it did not work.

It returned a blank page of course, so I went into the database to see if the customer record was updated. It was not.
 
Is the API URL correct? Is it expecting the JSON encoded data? Does it restrict any headers?

If you use something like postman.com - and send a manual cURL request to the $url with your encoded JSON - are you able to get the customer phone field to update?
 
Is the API URL correct? Is it expecting the JSON encoded data? Does it restrict any headers?

If you use something like postman.com - and send a manual cURL request to the $url with your encoded JSON - are you able to get the customer phone field to update?

1. API is correct.
2. Yes it's expecting JSON encoded data.
3. I do not think it's restricting any head.
4. I don't have postman.com

Just so you know, I wrote the code as I am trying to edit customer from https://developer.responsecrm.com/#edit-customer maybe this will help.

Just so you know, I done updated the API url. I just use the one above as an example.
 
Just so you know, I done updated the API url. I just use the one above as an example.

Ahh - I get it now! The API is expecting a PUT request - you've been making a POST request.

Here's an updated code - try this and let me know please

PHP:
<?php

$url = 'https://openapi.responsecrm.com/api/v2/open/customers/9271111';

$data = [
    "Phone" => "1234567890" // The new phone number you want to set
    ];

// Initialize a cURL session.
$curl = curl_init($url);

// JSON encode the data to be sent with the request.
$jsonData = json_encode($data);

// Set the necessary cURL options.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "Authorization: ApiKey DEMO"
]);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);

// Execute the cURL request and capture the response.
$response = curl_exec($curl);

// Check for errors in the cURL request.
if ($response === false) {
    echo 'Curl error: ' . curl_error($curl);
} else {
    // If the cURL call was successful, output the response.
    echo "Response from API: " . $response;
}

curl_close($curl);
 
Try this :

PHP:
$url = "https://openapi.api.com/api/v2/open/customers/10005182";
$curl = curl_init($url);
$data = array(
    "Phone" => "7656493111"
);
$data = json_encode($data);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: ApiKey DEMO', 'Content-Type: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
$resp = curl_exec($curl);
if(curl_errno($curl)){
    $error_message = curl_error($curl);
    echo "cURL Error: " . $error_message;
}
curl_close($curl);
print_r($resp);

Since you're "updating" a record and not adding a new one, you must use "PUT" instead of "POST"
 
Also thank you @FBGuru and @BlogPro you two are awesome and it's working now!

Where are you two from?
 
Back
Top