使用Guzzle

时间:2017-01-03 18:42:01

标签: php rest api guzzle eventbrite

我可以使用以下代码添加事件:

//Create client
$client = new Client([
    'base_uri' => 'https://www.eventbriteapi.com/v3/',
]);

$params = [
  'event.name.html' => $this->Name,
  'event.description.html' => $this->Description,
  'event.listed'  => false,
  'event.start.utc'  => $this->StartTime,
  'event.start.timezone'  => 'Europe/London',
  'event.end.utc'  => $this->EndTime,
  'event.end.timezone'  => 'Europe/London',
  'event.currency'  => 'GBP'
];

try {
  //Send new event request
  $res = $client->request('POST', "events/", [
    'query' => ['token' => env('EVENTBRITE_TOKEN', '')],
    'form_params' => $params
  ]);
} catch (GuzzleException $e) {
  return false;
}

但是,当我尝试使用在步骤1中创建的ID和以下代码更新事件时:

//Send update event request
$res = $client->request('POST', "events/$this->EventBriteID", [
  'query' => ['token' => env('EVENTBRITE_TOKEN', '')],
  'form_params' => $params
]);

它不会更新活动。请求看起来很成功。状态200与事件对象一起返回long,但是没有更新任何数据。信息保持与创建事件时提交的信息相同。我做错了什么,我发现很难调试,因为端点返回200状态。

这个请求在Postman中运行正常,所以我显然在Guzzle中做错了。

修改

请求输出$ response-> request()

Client {#289 ▼
  -config: array:8 [▼
    "base_uri" => Uri {#298 ▼
      -scheme: "https"
      -userInfo: ""
      -host: "www.eventbriteapi.com"
      -port: null
      -path: "/v3/"
      -query: ""
      -fragment: ""
    }
    "handler" => HandlerStack {#165 ▼
      -handler: StreamHandler {#292 ▼
        -lastHeaders: []
      }
      -stack: array:4 [▼
        0 => array:2 [▼
          0 => Closure {#293 ▼
            class: "GuzzleHttp\Middleware"
            parameters: {▼
              $handler: {▼
                typeHint: "callable"
              }
            }
            file: "/home/user/mysite/vendor/guzzlehttp/guzzle/src/Middleware.php"
            line: "54 to 69"
          }
          1 => "http_errors"
        ]
        1 => array:2 [▼
          0 => Closure {#296 ▼
            class: "GuzzleHttp\Middleware"
            parameters: {▼
              $handler: {▼
                typeHint: "callable"
              }
            }
            file: "/home/user/mysite/vendor/guzzlehttp/guzzle/src/Middleware.php"
            line: "148 to 150"
          }
          1 => "allow_redirects"
        ]
        2 => array:2 [▼
          0 => Closure {#295 ▼
            class: "GuzzleHttp\Middleware"
            parameters: {▼
              $handler: {▼
                typeHint: "callable"
              }
            }
            file: "/home/user/mysite/vendor/guzzlehttp/guzzle/src/Middleware.php"
            line: "27 to 43"
          }
          1 => "cookies"
        ]
        3 => array:2 [▼
          0 => Closure {#286 ▼
            class: "GuzzleHttp\Middleware"
            parameters: {▼
              $handler: {▼
                typeHint: "callable"
              }
            }
            file: "/home/user/mysite/vendor/guzzlehttp/guzzle/src/Middleware.php"
            line: "216 to 218"
          }
          1 => "prepare_body"
        ]
      ]
      -cached: null
    }
    "allow_redirects" => array:5 [▼
      "max" => 5
      "protocols" => array:2 [▼
        0 => "http"
        1 => "https"
      ]
      "strict" => false
      "referer" => false
      "track_redirects" => false
    ]
    "http_errors" => true
    "decode_content" => true
    "verify" => true
    "cookies" => false
    "headers" => array:1 [▼
      "User-Agent" => "GuzzleHttp/6.2.1 PHP/7.0.8-0ubuntu0.16.04.3"
    ]
  ]
}

3 个答案:

答案 0 :(得分:2)

所以经过几个小时的绝望,我终于解决了它。问题是端点url上缺少尾部斜杠:

$res = $client->request('POST', "events/$this->EventBriteID", []);

更改为

$res = $client->request('POST', "events/$this->EventBriteID/", []);

现在按预期工作。

答案 1 :(得分:1)

测试令牌和参数的一种好方法是导航到RapidAPI here.我已将您直接链接到EventbriteAPI updateEvent端点。您应该继续将参数和令牌填入表单并单击测试。您应该看到详细的JSON响应。下面是一个简单的例子:

enter image description here

如果一切看起来都运行正常,请点击仪表板中响应上方的CODE,注册,从下拉列表中选择PHP,RapidAPI将提供一个php代码片段,您可以直接将其复制并粘贴到您的代码中进行API调用。下面是RapidAPI将提供的PHP代码片段的示例:

require __DIR__ . '/vendor/autoload.php';
use RapidApi\rapidApiConnect;

$rapid = new RapidApiConnect("StackOverflowTest",       "##########################");

$result = $rapid->call('EventbriteAPI', 'updateEvent', [
    'token' => '####################',
    'eventId' => '31082267900',
    'eventName' => 'Ghost Tour Test',
    'eventStartUtc' => '2017-02-20T03:00:00Z',
    'eventStartTimezone' => 'America/Los_Angeles',
    'eventEndUtc' => '2017-02-20T06:00:00Z',
    'eventEndTimezone' => 'America/Los_Angeles',
    'eventCurrency' => 'USD',
    'eventDescription' => ''
]);

希望这有帮助!

答案 2 :(得分:1)

您最有可能使用Gu {而不是body的{​​{1}}请求选项。对请求使用form_params将发送form_params数据,这些数据通常现代API所期望的数据。在Eventbrite的情况下,我相信它需要一个定期的application/x-www-form-urlencoded编码请求。

将您的Guzzle请求更改为:

form-data

希望这有效!