2

I am trying to make a POST request to the GETResponse API(https://apidocs.getresponse.com/v3/case-study/adding-contacts) with thephpleague/oauth2-client and adespresso/oauth2-getresponse as a provider like so:

  $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);

I also tried this passing in content type value of application/json in the headers all to no avail.

$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); `

However the getParsedResponse function in both approaches returns the following:

League \ OAuth2 \ Client \ Provider \ Exception \ IdentityProviderException (400) UnsupportedContentTypeheader.
Paul Tofunmi
  • 435
  • 4
  • 15

2 Answers2

5

I know it's late, but try this code:

$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 );
Marcus Campbell
  • 2,746
  • 4
  • 22
  • 36
troxxx
  • 51
  • 2
  • This worked for me for Bitly API as well. One thing if you're debugging Bitly: `group_guid` is also required, even though documentation doesn't say so. – Paul Nowak May 05 '19 at 23:32
2

Passing POST data with json_encode() didn't work for me, I tried different solutions but that's the only one that worked for me

$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);
Andrea Mauro
  • 773
  • 1
  • 8
  • 14