0

this works in console but when i try in PHP doesn't work

$cURL = "curl -X POST -d 'client_id=".$CLIENT_ID."&client_secret=".$CLIENT_SECRET."&grant_type=authorization_code&code=".$CODE."' https://www.wrike.com/oauth2/token";

CODE PHP.

$postData = array("client_id" => $CLIENT_ID, 
            "client_secret" => $CLIENT_SECRET, 
            "grant_type" => "authorization_code",
            "code" => $CODE);

$handler = curl_init();  
curl_setopt($handler, CURLOPT_URL, $url);  
curl_setopt($handler, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($handler, CURLOPT_POST, sizeof($postData));  
curl_setopt($handler, CURLOPT_POSTFIELDS, $postData);  
$response = curl_exec ($handler);  
curl_close($handler);

When i run this code the result is "Resource id #2", the result i expect is this

{
   "access_token": "2YotnFZFEjr1zCsicMWpAA",
   "refresh_token": "tGzv3JOkF0XG5Qx2TlKWIA",
   "token_type": "bearer",
   "expires_in": 3600
}

in console works fine, but when i try in PHP doesn't work,

moloradio
  • 13
  • 5

3 Answers3

0

I'd recommend using https://github.com/rmccue/Requests which will make your life much easier. You code would then be:

        $postData = array("client_id" => $CLIENT_ID, 
            "client_secret" => $CLIENT_SECRET, 
            "grant_type" => "authorization_code",
            "code" => $CODE);
        $headers = array('Content-Type' => 'application/json');  // assuming you post JSON
        $response = Requests::post($$url, $headers, json_encode($obj));
        if ($response->status_code !== 200) {
             // handle OK
        } else {
             // handle ERROR
        }

The Requests library also handles cases where curl is not available in PHP; will return the full header, body, cookies etc. in one object.

mark
  • 344
  • 3
  • 8
0

A couple of things (some that commenters have already noted):

  • CURLOPT_POST should just be set to 1 not the data size
  • If you are seeing Resource id #2 you might be testing the result from $handler instead of $result.
  • While perhaps not necessary, you can use http_build_query on your data before sending to ensure the encoding is correct
  • It is always worth checking for any curl errors with curl_errno

A complete example:

$postData = array("client_id" => $CLIENT_ID, 
            "client_secret" => $CLIENT_SECRET, 
            "grant_type" => "authorization_code",
            "code" => $CODE);

$handler = curl_init();

curl_setopt($handler, CURLOPT_URL, $url);
curl_setopt($handler, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handler, CURLOPT_POSTFIELDS, http_build_query($postData));
curl_setopt($handler, CURLOPT_POST, 1);

$response = curl_exec($handler);
if (curl_errno($handler)) {
    echo 'Error:' . curl_error($handler);
}
curl_close ($handler);

var_dump($response);

Side note, curl-to-PHP is a tool that is handy for converting curl commands to PHP.

John C
  • 8,223
  • 2
  • 36
  • 47
0

UPDATE for everyone interested. I lately added a working package to thephpleague's library which allows you to connect with the Wrike Api:

https://github.com/michaelKaefer/oauth2-wrike

(Another thing to troubleshoot on this question could be PHP's SSL-configuration, more for example here: PHP cURL Not Working with HTTPS)

Michael Käfer
  • 1,597
  • 2
  • 19
  • 37