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

问题描述 投票:0回答:3

我的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网站传递数据时,每个人都得到一个身体响应,只有分页细节,没有事件详细信息......

谢谢

php json eventbrite php-curl
3个回答
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


0
投票

以下是对我有用的东西。 Insomnia / Postman指令,以及简单的PHP cURL。

终点:https://www.eventbriteapi.com/v3/batch/ 方法:发布 标题:内容类型:application / x-www-form-urlencoded Auth:Bearer Token 将正文传递为表单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;
}

0
投票

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

{"batch": "[{
    \"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\"
}]"
}

这是另一种选择,这应该可以解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.