dtuy84033 2015-03-06 18:40
浏览 62
已采纳

发送Curl POSTFIELDS数组

I am trying to use CURL to post the following fields to PANDADOCS, but for some reason I am getting an error that the values are not being received on their side.

This is the error I am getting:

"type": "validation_error", "detail": {"url": ["This field is required."], "name": ["This field is required."]}}

I am posting with the following:

$docurl = "myurl.com/document.pdf";
$headr = array();
$headr[] = 'Content-length: 0';
$headr[] = 'Content-Type: application/json;charset=UTF-8';
$headr[] = "Authorization: Bearer $ACCESS_TOKEN";
$url = 'https://api.pandadoc.com/public/v1/documents';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$headr);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);


$postfields = array();

$postfields['name'] = 'PSA';
$postfields['url'] = $docurl;
$postfields['recipients'] = array ([0]=>array(

            ['email'] => ['dondon@gmail.com'],
            ['first_name'] => ['don'],
            ['last_name'] => ['jones'],
            ['role']=>['u1']    ));



curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(  $postfields) );
$ret = curl_exec($ch); // 

when I print_r($postfields) I get Array ( [name] => PSA [url] => https://api.pandadoc.com/public/v1/documents [recipients] => Array ( ) )

so all the fields arent getting posted.

but whats wierd is that the URL and NAME are in the array but not the other fields yet the error is complaining about not receiving NAME and URL..

dazed and confused...

* Hostname was found in DNS cache
*   Trying 54.190.72.92...
* Connected to api.pandadoc.com (54.190.72.92) port 443 (#28)
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* Server certificate:
*    subject: OU=GT83522468; OU=See www.rapidssl.com/resources/cps (c)14; OU=Domain Control Validated - RapidSSL(R); CN=*.pandadoc.com
*    start date: 2014-11-09 00:32:24 GMT
*    expire date: 2016-10-11 09:34:58 GMT
*    subjectAltName: api.pandadoc.com matched
*    issuer: C=US; O=GeoTrust Inc.; CN=RapidSSL SHA256 CA - G3
*    SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway.
> POST /public/v1/documents HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2
Host: api.pandadoc.com
Accept: */*
Content-length: 0
Content-Type: application/json;charset=UTF-8
Authorization: Bearer [ACCESS TOKEN]

* upload completely sent off: 37 out of 37 bytes
< HTTP/1.1 400 BAD REQUEST
* Server nginx/1.4.6 (Ubuntu) is not blacklisted
< Server: nginx/1.4.6 (Ubuntu)
< Date: Fri, 06 Mar 2015 19:52:53 GMT
< Content-Type: application/json
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept
< Allow: GET, POST, DELETE, HEAD, OPTIONS
< 
* Connection #28 to host api.pandadoc.com left intact
  • 写回答

1条回答 默认 最新

  • duanmu3049 2015-03-06 18:47
    关注

    $string is not defined.

    Add

    $string = http_build_query( $postfields );
    

    after

    $postfields = array();
    
    $postfields['name'] = 'PSA';
    $postfields['url'] = $docurl;
    
    // This is invalid array
    $postfields['recipients'] = array ([0]=>array(
    
            ['email'] => ['dondon@gmail.com'],
            ['first_name'] => ['don'],
            ['last_name'] => ['jones'],
            ['role']=>['u1']    ));
    

    http://php.net/manual/en/function.http-build-query.php

    UPDATE

    I just read Pandadoc API. They accept json data and your data was invalid. Also content type.

    This should work:

    <?php
    
    $url                = 'https://api.pandadoc.com/public/v1/documents'; 
    $docurl             = "myurl.com/document.pdf";
    
    $postfields         = array();
    $postfields['name'] = 'PSA';
    $postfields['url']  = $docurl;
    $postfields['recipients'] = array(
        array(
                'email'      => 'dondon@gmail.com',
                'first_name' => 'don',
                'last_name'  => 'jones',
                'role'       => 'u1'
            )
    );
    $data_string = json_encode( $postfields );
    
    $headr = array();
    $headr[] = 'Content-length: '.strlen( $data_string );
    $headr[] = 'Content-type: application/json';
    $headr[] = "Authorization: Bearer $ACCESS_TOKEN";
    
    $ch = curl_init( $url );
    
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headr );
    
    $result = curl_exec( $ch );
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)