4

I'm having PHP curl script that send some POST variables to CloudFlare API: My fields are:

    $fields = array(
        'type' => 'A',
        'name' => $subdomain.rand(1, 2000),
        'content' => Env::getCFIP(),
        'proxied' => true,
        'ttl' => 1
    );

And the curl curl_setopt are:

        array(
            CURLOPT_POST            => true,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_POSTFIELDS      => $fields,
            CURLOPT_RETURNTRANSFER  => 1,
            CURLOPT_TIMEOUT         => 3,
            CURLOPT_VERBOSE         => 0,
            CURLOPT_FORBID_REUSE    => true,
            CURLOPT_HTTPHEADER      => array(
                'X-Auth-Email: '.Env::getCFEmail(),
                'X-Auth-Key: '.Env::getCFAuthKey(),
            )
        );

Default is value of 'proxied' is false. If I skip it in data fields, scripts works. However, I want to have it on true, but I get error:

    object(stdClass)[2]
      public 'success' => boolean false
      public 'errors' => 
        array (size=1)
          0 => 
            object(stdClass)[8]
              public 'code' => int 1004
              public 'message' => string 'DNS Validation Error' (length=20)
              public 'error_chain' => 
                array (size=1)
                  0 => 
                    object(stdClass)[7]
                       public 'code' => int 9003
                       public 'message' => string 'Invalid 'proxied' value, must be a boolean' (length=42)
      public 'messages' => 
        array (size=0)
          empty
      public 'result' => null

So my guess is that when cURL send POST request it converts data variables to STRING. Is there any way to stop this behavior?

4
  • you can send 1 for true, and 0 for false for this. Commented Sep 7, 2017 at 22:25
  • @tanaydin , setting it up as 1, '1', 'true', 'True', does not work. Commented Sep 7, 2017 at 22:27
  • when you get back, convert it to integer and it could work. Commented Sep 7, 2017 at 22:31
  • @tanaydin casting it to bool, int, or casting $fields array as object do not work. I still got the same error. Commented Sep 7, 2017 at 22:44

1 Answer 1

2

I manage to solve that one by:

  1. Encoding fields as json:

        CURLOPT_POSTFIELDS      => json_encode($fields),
    
  2. Adding 'Content-Type: application/json' in headers

        CURLOPT_HTTPHEADER      => array(
            'X-Auth-Email: '.Env::getCFEmail(),
            'X-Auth-Key: '.Env::getCFAuthKey(),
            'Content-Type: application/json',
    
Sign up to request clarification or add additional context in comments.

1 Comment

I was going to say, payloads via cURL are normally JSON or XML format, depending on the API endpoint.

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.