POST using cURL and x-www-form-urlencoded in PHP returning Access Denied

VaTo

I have been able to use the Advanced Rest Client Extension for chrome to send POST queries to an specific HTTPS server and I get Status Code: 200 - OK with the same body fields as the ones I used in this code, but when I run the following code I get this response: 403 - Access Denied.

<?php
$postData = array(
'type' => 'credentials',
'id' => 'exampleid',
'secret_key' => 'gsdDe32dKa'
);

// Setup cURL
$ch = curl_init('https://www.mywebsite.com/oauth/token');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));

// Send the request
$response = curl_exec($ch);

var_dump($response);

// Check for errors
if($response === FALSE){
die(curl_error($ch));
}

// Decode the response
$responseData = json_decode($response, TRUE);

// Print the date from the response
echo $responseData['published'];
?>

I've noticed as well that when I use Advanced Rest Client Extension for chrome and if I set the Content-Type to application/json I have to enter a login and a password that I don't know what are those because even if I enter the id and secret key that I have in the code it returns 401 Unauthorized. So I'm guessing this code that I wrote is not forcing it to the content-type: application/x-www-form-urlencoded, but I'm not sure. Thank you for any help on this issue!

Antoan Milkov

Can you try like that and see if it helps:

curl_setopt_array($ch, array(
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_COOKIEFILE => 'cookie.txt',
    CURLOPT_COOKIEJAR => 'cookie.txt',
    CURLOPT_USERPWD => 'username:password', //Your credentials goes here
    CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'),
    CURLOPT_POSTFIELDS => http_build_query($postData),
));

I guess the site expect simple authentication on top of the secret_key that you already provided.
Also it is possible to send a Cookie, so just in case it is good idea to store it and use it again in the next Curl calls.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to post body x-www-form-urlencoded using webclient?

How to do a post request using FORM-DATA or x-www-form-urlencoded with Android?

cURL send JSON as x-www-form-urlencoded

How to do a x-www-form-urlencoded POST login using cypress?

How to force Angular2 to POST using x-www-form-urlencoded

POST application/x-www-form-urlencoded Body to REST API using Jitterbit

How do I post data using okhttp library with content type x-www-form-urlencoded?

Server-side Blazor Post using HttpClient with x-www-form-urlencoded

How to POST with application/x-www-form-urlencoded header and URLSearchParams using isomorphic-fetch

Send sensitive data using POST+application/x-www-form-urlencoded

How to post (x-www-form-urlencoded) Json Data using Retrofit?

How to post a x-www-form-urlencoded data properly using javascript?

Trying to make an API POST using application/x-www-form-urlencoded in a wpf desktop app

How to post an array of values using `UrlFetchApp.fetch` with `application/x-www-form-urlencoded`?

Jersey client Post Request with x-www-form-urlencoded Fails

How to POST x-www-form-urlencoded in retrofit

Spring - wrong encoding POST request with x-www-form-urlencoded

Post a x-www-form-urlencoded request from React Native

How to send post request with x-www-form-urlencoded body

post application/x-www-form-urlencoded Alamofire

Swift 4 send POST request as x-www-form-urlencoded

RestSharp post request - Body with x-www-form-urlencoded values

How to POST content as application/x-www-form-urlencoded

How to send x-www-form-urlencoded in a post request in webclient?

Axios post form urlencoded requests application/x-www-form-urlencoded

application/x-www-form-urlencoded between ionic and php

POST using HttpClient fro content type application/x-www-form-urlencoded using grant_type and scope

Access Denied for CURL Post

Retrofit + POST method + www-form-urlencoded