Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with POST json data #54

Open
stanleyliu1006 opened this issue Jan 29, 2016 · 2 comments
Open

Issue with POST json data #54

stanleyliu1006 opened this issue Jan 29, 2016 · 2 comments

Comments

@stanleyliu1006
Copy link

Hi

I am using the snippet code below for POST an new event, but failed with 400 Error. just wondering if I missed something or can you please provide me the sample code of POST data?

My email address is: [email protected]

setAccessTokenType(2,$client->getClientSecret()); $param ='{"event":{"name":"ACF API Test","intro":"protect our environment","start_time":"2016-02-08T17:00:00-00:00","end_time":"2016-02-08T19:00:00-00:00","status":"unlisted"}}'; $response = $client->fetch($this->nb_baseapiUrl . '/api/v1/events/', $param, 'POST'); print_r($response); ?>

Regards
Stanley

@bcgoodmate
Copy link

This library was not prepared to POST a data in JSON format using CURL

Here's a working sample:

$params = array(
    "firstName" => "John",
    "lastName" => "Doe",
    "email1" => array(
        "value" => "[email protected]",
        "default" => true
     )                          
);

$header = array( 
    'Content-Type' => 'application/json;charset=utf-8', 
    'Content-Length' => strlen(json_encode($params))
);

$response = $client->fetch('https://graph.facebook.com/me', $params, 'PUT', $header);

var_dump($response);

Modifications
Function: executeRequest()
File: Client.php
Line: 424 to 427

From this code:

if(is_array($parameters) && self::HTTP_FORM_CONTENT_TYPE_APPLICATION === $form_content_type) {
    $parameters = http_build_query($parameters, null, '&');
}
$curl_options[CURLOPT_POSTFIELDS] = $parameters;

Replace it with this code:

if(strpos($http_headers['Content-Type'], 'x-www-form-urlencoded') !== false) {
    if(is_array($parameters) && self::HTTP_FORM_CONTENT_TYPE_APPLICATION === $form_content_type) {
        $parameters = http_build_query($parameters, null, '&');
    }
    $curl_options[CURLOPT_POSTFIELDS] = $parameters;
} else {
    $curl_options[CURLOPT_POSTFIELDS] = json_encode($parameters);

    if (is_array($parameters) && count($parameters) > 0) {
        $url .= '?' . http_build_query($parameters, null, '&');
    } elseif ($parameters) {
        $url .= '?' . $parameters;
    }
}

This was referenced Apr 4, 2016
@DanMan
Copy link

DanMan commented Oct 27, 2022

By now, you can also pass the $parameters already JSON encoded AFAIK, if you also provide the JSON Content-Type header. You're already encoding it for the Content-Length header anyway. At least as long as you're not also using ACCESS_TOKEN_URI. That currently throws an exception in the Client::fetch() method.

koyan added a commit to outlandishideas/PHP-OAuth2 that referenced this issue Jan 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants