调用Eventbrite API返回404错误的PHP代码问题求助
问题分析与解决方案
核心问题
你遇到的404错误是因为API端点URL构造错误,同时认证方式不符合Eventbrite API的要求:
- 原URL中
events=是无效的路径写法,正确的组织活动端点应该以/events/结尾 - Token没有以正确的方式传递(直接拼在路径后会被识别为路径的一部分,而非认证参数)
修正步骤
1. 修正API端点URL
正确的组织活动列表端点格式为:
https://www.eventbriteapi.com/v3/organizations/{ORGANIZATION_ID}/events/
2. 正确传递认证Token
推荐使用Bearer Token放在请求头(更安全规范),或者通过查询参数?token=XXX传递。
3. 修正后的完整代码
<?php ini_set('display_errors', '1'); $token = 'XYZ123456XYZ'; $orgId = '123456789'; // 正确的API端点 $url = "https://www.eventbriteapi.com/v3/organizations/{$orgId}/events/"; // Initialize cURL $ch = curl_init(); // Set the cURL options curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 添加Bearer Token认证头 curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer {$token}" ]); // Execute the cURL request and get the response $response = curl_exec($ch); // 检查cURL请求本身是否失败 if(curl_errno($ch)) { echo 'cURL Error: ' . curl_error($ch); curl_close($ch); exit; } // Print the raw response to the console echo '<pre>'; print_r($response); echo '</pre>'; // Close the cURL session curl_close($ch); // Parse the JSON response $events = json_decode($response, true); // Check for errors if (isset($events['error'])) { echo 'Error: ' . $events['error_description']; } else { // Loop through the events and display their information foreach ($events['events'] as $event) { echo '<h2>' . htmlspecialchars($event['name']['text']) . '</h2>'; // 处理可能为空的描述 $description = isset($event['description']['text']) ? $event['description']['text'] : '无描述'; echo '<p>' . htmlspecialchars($description) . '</p>'; echo '<p>开始时间:' . $event['start']['local'] . '</p>'; echo '<p>结束时间:' . $event['end']['local'] . '</p>'; // 处理可能为空的场地信息 $venueName = isset($event['venue']['name']) ? $event['venue']['name'] : '未设置场地'; echo '<p>场地:' . htmlspecialchars($venueName) . '</p>'; echo '<hr>'; } } ?>
额外注意事项
- 确保你的
$orgId(123456789)是真实有效的组织ID,可通过Eventbrite后台或API的用户组织查询端点确认 - 确认你的Token拥有读取该组织活动的权限
- 添加
htmlspecialchars()防止XSS注入,确保输出内容安全
内容的提问来源于stack exchange,提问作者Anando DasGupta
相关产品推荐
相关产品推荐

