【发布时间】:2019-11-06 18:22:55
【问题描述】:
我正在尝试在 Guzzle 中复制 CURL POST 请求,但 Guzzle 请求失败。
这是成功运行的 CURL 请求:
$file = new \CURLFile( $document );
$file->setPostFilename( basename( $document ) );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_URL, $endpoint );
curl_setopt( $ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer " . $accessToken,
"Content-Type: multipart/form-data",
] );
curl_setopt( $ch, CURLOPT_POSTFIELDS, [ 'fileData' => $file ] );
$response = curl_exec( $ch );
这是我目前用于 Guzzle 请求的内容,但它不起作用:
$options['multipart'][] = [
'name' => 'fileData',
'contents' => fopen( $document, 'r' ),
'filename' => basename( $document ),
];
$request = $provider->getAuthenticatedRequest( 'POST', $endpoint, $accessToken, $options );
$response = $provider->getParsedResponse( $request );
Guzzle 请求的响应如下:
{"message":"File cannot be empty","errors":[{"code":"Missing","fields":["document"]}]}
值得注意的是,我使用thephpleague/oauth2-client 库来发送请求。我正在寻找这两个请求之间的任何差异或有关如何自己进一步解决此问题的信息,因为我整天都在旋转我的车轮。非常感谢
【问题讨论】: