Oauth 2.0 使用Guzzle/League OAuth2客户端复制CURL多部分/表单数据请求

Oauth 2.0 使用Guzzle/League OAuth2客户端复制CURL多部分/表单数据请求,oauth-2.0,guzzle,Oauth 2.0,Guzzle,我试图在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

我试图在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"]}]} 

值得注意的是,我正在使用库发送请求。我正在寻找这两个请求之间的任何差异,或者我自己如何进一步解决这个问题的信息,因为我已经在这个问题上转了一整天了。非常感谢

HPLeague/oauth2客户端使用不同的提供者创建请求,这些提供者实现请求

的参数$options不同于:

$options只允许使用“headers”、“body”和“protocolVersion”键

您应该付出额外的努力,创建所需的标题和正文:

$file = new \CURLFile( $document );
$file->setPostFilename( basename( $document ) );
$data = array(
    'uploaded_file' => $file
);
$options = array(
    'headers' => array("Content-Type" => "multipart/form-data"),
    'body' => $data
);
$request = $provider->getAuthenticatedRequest( 'POST', $endpoint, $accessToken, $options );
参考文献

$file = new \CURLFile( $document );
$file->setPostFilename( basename( $document ) );
$data = array(
    'uploaded_file' => $file
);
$options = array(
    'headers' => array("Content-Type" => "multipart/form-data"),
    'body' => $data
);
$request = $provider->getAuthenticatedRequest( 'POST', $endpoint, $accessToken, $options );