PHP cURL发送bool POST变量

我正在使用
PHP curl脚本向CloudFlare API发送一些POST变量:

我的领域是:

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

curl curl_setopt是:

        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(),
            )
        );

默认值为’proxied’为false.如果我在数据字段中跳过它,脚本可以工作.但是,我希望它是真的,但我得到错误:

    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

所以我的猜测是,当cURL发送POST请求时,它会将数据变量转换为STRING.有没有办法阻止这种行为?

最佳答案 我设法通过以下方式解决这个问题:

>将字段编码为json:

    CURLOPT_POSTFIELDS      => json_encode($fields),

>在标题中添加’Content-Type:application / json’

    CURLOPT_HTTPHEADER      => array(
        'X-Auth-Email: '.Env::getCFEmail(),
        'X-Auth-Key: '.Env::getCFAuthKey(),
        'Content-Type: application/json',
点赞