编程语言
首页 > 编程语言> > php-使用Guzzle将数据更新为EventBrite API

php-使用Guzzle将数据更新为EventBrite API

作者:互联网

我可以使用带有以下代码的post添加事件:

//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;
}

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

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

它不会更新事件.该请求看起来成功.状态200与事件对象一起返回的时间很长,但是没有数据被更新.信息与创建事件时提交的信息相同.我做错了,我发现很难调试,因为端点返回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"
    ]
  ]
}

解决方法:

因此,经过数小时的绝望,我终于解决了它.问题是端点URL上缺少尾部斜杠:

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

变成

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

现在可以正常工作了.

标签:guzzle,rest,api,eventbrite,php
来源: https://codeday.me/bug/20191026/1936105.html