PHP cURL发送bool POST变量

时间:2017-09-07 22:23:56

标签: php api curl cloudflare

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

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

curl curl_setopt 是:

    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

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

<img>

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

1 个答案:

答案 0 :(得分:1)

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

  1. 将字段编码为 json

        CURLOPT_POSTFIELDS      => json_encode($fields),
    
  2. 在标题中添加&#39;内容类型:application / json&#39;

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