Mr. Doge
Mr. Doge

Reputation: 23

Why does my POST request time out in PHP using cURL, but not in Postman?

I have an auth.php file that should make a request to an API with some headers, data and stuff.

I tried Postman, and gave me a response almost immediately.

I copied the code (PHP > cURL) and tried it, and it would be waiting for MYPRIVATESITE.com for 30 seconds (I set the timeout to that), and then just cURL ERROR: TIMED OUT (or something like that).

What did I do wrong? It works with e.g. postman, so why not my website?

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => "https://discordapp.com/api/v6/oauth2/token",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => "client_id=PRIVATEID&client_secret=PRIVATEKEY&grant_type=authorization_code&code=$code&redirect_uri=https%3A%2F%2Fkanebot.epizy.com%2Fauth.php&scope=identify%20guilds&undefined=",
    CURLOPT_HTTPHEADER => array(
        "Content-Type: application/x-www-form-urlencoded",
        "cache-control: no-cache"
    )
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}

Note: The PRIVATEKEY and PRIVATEID are there, I just remove them because I don't want anyone else to steal it. It's defined, and it worked (read up). The $code is also defined.

Upvotes: 1

Views: 1072

Answers (1)

ron
ron

Reputation: 826

You are missing the & operator in your POSTFIELDS between the client_secret and the grant_type

try to add the & and see if its working after (it will sure solve one of the problems you have)

client_id=PRIVATEID&client_secret=PRIVATEKEYgrant_type=authorization_code&code=$code&redirect_uri=https%3A%2F%2Fkanebot.epizy.com%2Fauth.php&scope=identify%20guilds&undefined=

Upvotes: 1

Related Questions