发送Curl POSTFIELDS数组

时间:2015-03-06 18:40:44

标签: php arrays curl

我正在尝试使用CURL将以下字段发布到PANDADOCS,但由于某种原因,我收到的错误是没有收到值。

这是我得到的错误:

“type”:“validation_error”,“detail”:{“url”:[“此字段为必填项。”],“名称”:[“此字段为必填项。”]}}

我发布的内容如下:

$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); // 

当我print_r($ postfields) 我明白了 数组([名称] => PSA [网址] => https://api.pandadoc.com/public/v1/documents [收件人] =>数组())

因此所有字段都没有发布。

但最奇怪的是URL和NAME是在数组中而不是其他字段但错误是抱怨没有收到NAME和URL ..

感到茫然和困惑......

* 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 个答案:

答案 0 :(得分:0)

$string未定义。

添加

$string = http_build_query( $postfields );

之后

$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

<强>更新

我刚读过Pandadoc API。他们接受json数据,您的数据无效。也是内容类型。

这应该有效:

<?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 );

?>