【问题标题】:Making POST REQUEST with thephpleague/oauth2-client使用 phpleague/oauth2-client 发出 POST REQUEST
【发布时间】:2019-03-14 02:36:06
【问题描述】:

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

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

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

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

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

League \ OAuth2 \ Client \ Provider \ Exception \ IdentityProviderException (400) UnsupportedContentTypeheader.

【问题讨论】:

    标签: oauth-2.0 request guzzle6 guzzle getresponse


    【解决方案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 );
    

    【讨论】:

    • 这对我也适用于 Bitly API。如果您正在调试 Bitly,一件事:group_guid 也是必需的,即使文档没有这么说。
    【解决方案2】:

    使用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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多