Eventbrite Batch Endpoint - 您传递了一个非JSON格式的请求主体

时间:2017-09-30 14:53:07

标签: php json eventbrite php-curl

我的JSON低于哪个验证正常(数字是虚拟数字!)。此字符串分配给变量$payload

[{
    "method": "GET",
    "relative_url": "series/4/events/?expand=ticket_classes"
}, {
    "method": "GET",
    "relative_url": "series/5/events/?expand=ticket_classes"
}, {
    "method": "GET",
    "relative_url": "series/6/events/?expand=ticket_classes"
}, {
    "method": "GET",
    "relative_url": "series/7/events/?expand=ticket_classes"
}, {
    "method": "GET",
    "relative_url": "series/8/events/?expand=ticket_classes"
}, {
    "method": "GET",
    "relative_url": "series/9/events/?expand=ticket_classes"
}]

我的PHP如下:

    $url = "https://www.eventbriteapi.com/v3/batch/?token=".$token;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$payload);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($payload))                                                                       
    );
    curl_setopt($ch, CURLOPT_POST, 1);
    $body = \curl_exec($ch);
    curl_close($ch);
    $json = \json_decode($body,true);
    echo $body;

回声正在吐出:

{"status_code": 400, "error_description": "You passed a request body that was not in JSON format.", "error": "INVALID_BODY"}

一切似乎都很好 - 我错过了一些明显的东西?

当我通过https://www.eventbriteapi.com网站传递数据时,对于每个网站,我都会得到一个只有分页详情且没有活动详情的正文回复......

由于

3 个答案:

答案 0 :(得分:0)

编辑HTTPClient.php

替换它:

   $options = array(
        'http'=>array(
            'method'=>$httpMethod,
            'header'=>"content-type: application/json\r\n",
            'content'=>$data,
            'ignore_errors'=>true
        )
    );

有了这个:

    $options = array(
        'http'=>array(
            'method'=>$httpMethod,
            'header'=>"content-type: application/json\r\n",
            'ignore_errors'=>true
        )
    );

    if ($httpMethod == 'POST') {
        $options['http']['content'] = $data;
    }

参考:https://github.com/eventbrite/eventbrite-sdk-php/issues/7#issuecomment-308537756

答案 1 :(得分:0)

以下是对我有用的。既有Insomnia / Postman指令,也有简单的PHP cURL。

端点https://www.eventbriteapi.com/v3/batch/
方法:发布
标题:内容类型:application / x-www-form-urlencoded
验证:不记名令牌
将正文作为表单URL编码传递
名称:批处理
值:

[    {"method":"GET", "relative_url":"events/EVENT_ID/attendees?page=1"},    {"method":"GET", "relative_url":"events/EVENT_ID_HERE/attendees?page=2"},   {"method":"GET", "relative_url":"events/EVENT_ID_HERE/attendees?page=3"},   {"method":"GET", "relative_url":"events/EVENT_ID_HERE/attendees?page=3"}  ]

简单的PHP cURL


curl_setopt_array($curl, array(
  CURLOPT_URL => "https://www.eventbriteapi.com/v3/batch/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "batch=%5B%20%09%20%7B%22method%22%3A%22GET%22%2C%20%22relative_url%22%3A%22events%2FEVENT_ID_HERE%2Fattendees%3Fpage%3D1%22%7D%2C%20%09%20%7B%22method%22%3A%22GET%22%2C%20%22relative_url%22%3A%22events%2FEVENT_ID_HERE%2Fattendees%3Fpage%3D2%22%7D%2C%20%09%20%7B%22method%22%3A%22GET%22%2C%20%22relative_url%22%3A%22events%2FEVENT_ID_HERE%2Fattendees%3Fpage%3D3%22%7D%2C%20%09%20%7B%22method%22%3A%22GET%22%2C%20%22relative_url%22%3A%22events%2FEVENT_ID_HERE%2Fattendees%3Fpage%3D3%22%7D%20%20%5D&=",
  CURLOPT_HTTPHEADER => array(
    "authorization: Bearer TOKEN GOES HERE",
    "content-type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

答案 2 :(得分:0)

看起来这是一个非常老的问题。但是,对于遇到此问题的其他任何人。使用“ Content-Type:应用程序/ json”时,您将希望传递一个双重编码的JSON对象。

read -a rows < data.txt                                          
for row in "${rows[@]}";do                                                      
  row_array=(${row})                                                            
  first=${row_array[0]} 
  sec=${row_array[1]}                                                        
  echo ${first}
  echo ${sec}                                                                 
done

这是另一个选项,可以解决此问题。