使用phpleague / oauth2-client进行POST请求

时间:2018-10-09 10:33:56

标签: oauth-2.0 request guzzle6 guzzlehttp getresponse

我正在尝试使用phpleague / oauth2-client和adespresso / oauth2-getresponse作为提供程序,向GETResponse API(https://apidocs.getresponse.com/v3/case-study/adding-contacts)发出POST请求,像这样:

http

我也尝试过在标头中传入application / json的内容类型值,但无济于事。

  $data = [
            'email' => $email,
            'campaign' => [
                'campaignId' => $campId
            ]
        ];
    $request = $this->provider->getAuthenticatedRequest(
                        'POST',
                        'https://api.getresponse.com/v3/contacts',
                        $this->getMyAccessToken(),
                        $data
            );
    $response = $this->provider->getParsedResponse($request);

但是,两种方法中的getParsedResponse函数都会返回以下内容:

$data = [ 'email' => $email, 'campaign' => [ 'campaignId' => $campId ] ];

    `$options['body'] = json_encode($data);
    $options['headers']['Content-Type'] = 'application/json';
    $options['headers']['access_token'] = $this->getMyAccessToken();
    $request = $this->provider->getAuthenticatedRequest(
                        'POST',
                        'https://api.getresponse.com/v3/contacts',
                        $options
            );
    $response = $this->provider->getParsedResponse($request); `

2 个答案:

答案 0 :(得分:1)

我知道已经晚了,但是尝试下面的代码:

$data = array(
  'email' => $email,
  'campaign' => array([
    'campaignId' => $campId
    ])
);

$options['body'] = json_encode( $data );
$options['headers']['Content-Type'] = 'application/json';
$options['headers']['Accept'] = 'application/json';
$request = $this->provider->getAuthenticatedRequest( 'POST', 'https://api.getresponse.com/v3/contacts', $this->getMyAccessToken(), $options );
$response = $this->provider->getParsedResponse( $request );

答案 1 :(得分:0)

使用json_encode()传递POST数据对我不起作用,我尝试了其他解决方案,但这是唯一对我有用的解决方案

$postData = [
    'date' => $date,
    'service_ids' => $serviceId,
];
$options = [
    'body' => http_build_query($postData),
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ],
];

$request = $this->provider->getAuthenticatedRequest("POST", $requestUrl, $token, $options);
$response = $this->provider->getParsedResponse($request);