Andurit
Andurit

Reputation: 5762

CURL x-www-form-urlencoded request - PHP

I'm trying to write simple CURL request from server. Script looks like:

$ch = curl_init();  
$url = 'http://URL/';
curl_setopt($ch, CURLOPT_URL, $url);
// ADD URL
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json',
'Authorization: Basic Y291bn54adsASD78asd5LWFkbWlu'));
// ADD HEADER
curl_setopt($ch, CURLOPT_POST, 1);
// SAY IT'S POST
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);    

if($response === false){
    echo 'Curl error: ' . curl_error($ch);
}
else{
    $sessionID = json_decode($response, true);
    print_r($sessionID);
}
curl_close($ch);

Problem is that I need to add some x-www-form-urlencoded like: grant_type = value, login = value2, password=value3. But I just can't make it work.

Can somebody help please?

Upvotes: 2

Views: 9700

Answers (1)

The values must be like they would be in an URL (hence the urlencoded), so:

grant_type=value&login=value2&password=value3

If values are not numbers, you will probably need to enclose them between quotes.

Upvotes: 1

Related Questions