5

I'm working with an API and when I send the POST request to them they throw a warning that one of the fields in the array has to be type Integer, not a String.

My CURL setup is like this:

$post_fields = array(
            'data_source_uuid' => $uuid,
            'name' => 'TestPlan',
            'interval_count' => 1,
            'interval_unit' => 'month',
            'external_id' => 'Eur_fees'
        );
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_URL => $url,
        CURLOPT_USERPWD => $api_key
        CURLOPT_POSTFIELDS =>  $post_fields, 
        CURLOPT_HTTPHEADER => 'Content-Type: application/json'
    ));
    $result = curl_exec($curl);
    curl_close( $curl );

When I send this to another URL on my localhost and var_dump it I get this:

    string(253) "array(5) {
          ["data_source_uuid"]=>
          string(39) "uuid"
          ["name"]=>
          string(8) "TestPlan"
          ["interval_count"]=>
          string(1) "1"
          ["interval_unit"]=>
          string(5) "month"
          ["external_id"]=>
          string(8) "Eur_fees"
        }"

The problem here is interval_count is a String not an Integer. If I var_dump before using CURLOPT_POSTFIELDS it is an Integer so something in the CURL part is changing it but I'm not sure what.

The API is for a website called chartmogul.com

2
  • secure.php.net/… Commented Aug 18, 2016 at 8:31
  • That doesn't work as it is an Int before the CURLOPT_POSTFIELDS, it's not being preserved Commented Aug 18, 2016 at 8:58

2 Answers 2

2

As the documentation says here (https://dev.chartmogul.com/docs/import-plan), you have to send JSON data.

Instead of CURLOPT_POSTFIELDS => $post_fields, you should use CURLOPT_POSTFIELDS => json_encode($post_fields)

EDIT : Also, the documentation says that you have to send 5 parameters, you forgot one required named "data_source_uuid", a string that contains the ChartMogul UUID of the data source for this subscription plan.

Sign up to request clarification or add additional context in comments.

8 Comments

That doesn't work. When I do that the message back is "error":"The property '#/' did not contain a required property of 'data_source_uuid'". I think that is because CURLOPT_POSTFIELDS does automatic urlencoding
Yes because you forgot to send "data_source_uuid" parameter (that shoud contain the ChartMogul UUID of the data source for this subscription plan).
Nope. I left that out of my example but when I put that in the array before your suggestion it doesn't work. I'll update my question to include that now.
Can you show the json_encode of your $post_fields please ? (don't forget to occult your uuid)
{"data_source_uuid":"uuid_xyz","name":"TestPlan","interval_count":1, "interval_unit":"month","external_id":"Eur_fees"} That is from before I use CURLOPT_POSTFIELDS. When I echo it just after the curl_exec line it's this: {"data_source_uuid":"uuid_xyz","name":"TestPlan","interval_count": "1","interval_unit":"month", "external_id":"Eur_fees"}
|
1

Bill from ChartMogul here. You would need to encode your data with json_encode($data). Please also ensure that your data source UUID, account secret key and account token are correct. The following request works for me:

<?php 

// account variables
$ds_uuid = "DATA_SOURCE_UUID";
$token = 'API_TOKEN';
$password = 'SECRET_KEY';

// request url
$baseurl='https://api.chartmogul.com/v1/';
$url=$baseurl.'import/plans';

// data to be posted
$post_fields = array(
            'data_source_uuid' => "$ds_uuid",
            'name' => 'A plan',
            'interval_count' => 1,
            'interval_unit' => 'month',
            'external_id' => 'eur_fees'
        );

// encode json data
$data = json_encode($post_fields);

// initialize cURL
$ch = curl_init();

// set options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$token:$password");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data))
);

// make the request
$result = curl_exec($ch);

// decode the result
$json = json_decode($result, true);

// print the result
print $json;

curl_close($ch);
?>`enter code here`

1 Comment

By the way, ChartMogul now has a PHP client library that you can use: github.com/chartmogul/chartmogul-php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.