Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a cURL POST request in PHP

Tags:

php

curl

I'm trying to figure out how to create a PHP cURL POST request. I have the following code to use. I'm a bit stuck on what the -g -X and -H means.

curl -g -X POST -H 'Authorization: bearer <access_token>' -d 'superTasks=["IEAE34A4KQATKP4S"]&metadata=[{"key":"testMetaKey","value":"testMetaValue"}]&priorityBefore=IEAE34A4KQATKP4S&importance=High&customFields=[{"id":"IEAE34A4JUAADKT3","value":"testValue"}]&description=Test task description&dates={"start":"2016-05-18","due":"2016-05-25"}&title=Test task&follow=true&followers=["KUAHYFH4"]&responsibles=["KUAHYFH4"]&shareds=["KUAHYFH4"]&parents=["IEAE34A4I4ATKP4L"]&status=Active' 'https://www.wrike.com/api/v3/folders/IEAE34A4I4ATKP4L/tasks'

Any help is greatly appreciated.

EDIT Added code below:

$postData = array(
    "title" => "Test task",
    "description" => "Test task description",
    "status" => "Active",
    "importance" => "Normal",
    "dates" => {"start":"2016-05-18","due":"2016-05-25"},
    "shareds" => ["KUAHYFH4"],
    "parents" => ["IEAE34A4I4ATKP4L"],
    "responsibles" => ["KUAHYFH4"],
    "followers" => ["KUAHYFH4"],
    "follow" => "true",
    "priorityBefore" => "IEAE34A4KQATKP4S",
    "priorityAfter" => "IEAE34A4KQATKP4S",
    "superTasks" => "["IEAE34A4KQATKP4S"]",
    "metadata" => "[{"key":"testMetaKey","value":"testMetaValue"}]"),
    "customFields" => [{"id":"IEAE34A4JUAADKT3","value":"testValue"}],
    "customStatus" => "string"
    );

$handler = curl_init();

curl_setopt($handler, CURLOPT_URL, "https://www.wrike.com/api/v3/folders/IEAE34A4I4ATKP4L/tasks");
curl_setopt($handler, CURLOPT_POSTFIELDS, http_build_query($postData));
curl_setopt($handler, CURLOPT_POST, true);
curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, false);

curl_exec($handler);

By doing curl -h I got the following information:

-g, --globoff Disable URL sequences and ranges using {} and []

-X, --request COMMAND Specify request command to use

-H, --header LINE Pass custom header LINE to server (H)

-d, --data DATA HTTP POST data (H)

But I can't figure out how to add these attributes.

like image 347
Matt Leach Avatar asked Nov 27 '22 14:11

Matt Leach


1 Answers

The Problem could be in the way you have formatted the values in the $postData array. All values including the JSON text should be in single quotes. Look at how I did it though the response was an error which I believe is a valid error

$postData = array(
    "title" => "Test task",
    "description" => "Test task description",
    "status" => "Active",
    "importance" => "Normal",
    "dates" => '{"start":"2016-05-18","due":"2016-05-25"}',
    "shareds" => '["KUAHYFH4"]',
    "parents" => '["IEAE34A4I4ATKP4L"]',
    "responsibles" => '["KUAHYFH4"]',
    "followers" => '["KUAHYFH4"]',
    "follow" => "true",
    "priorityBefore" => "IEAE34A4KQATKP4S",
    "priorityAfter" => "IEAE34A4KQATKP4S",
    "superTasks" => '["IEAE34A4KQATKP4S"]',
    "metadata" => '[{"key":"testMetaKey","value":"testMetaValue"}]',
    "customFields" => '[{"id":"IEAE34A4JUAADKT3","value":"testValue"}]',
    "customStatus" => "string"
    );

$handler = curl_init();
$access_token = "<access_token>";
$headers[] = 'Authorization: bearer '.$access_token;


curl_setopt($handler, CURLOPT_URL, "https://www.wrike.com/api/v3/folders/IEAE34A4I4ATKP4L/tasks");
curl_setopt($handler, CURLOPT_POSTFIELDS, http_build_query($postData));
curl_setopt($handler, CURLOPT_HTTPHEADER,$headers);
curl_setopt($handler, CURLOPT_POST, true);
curl_setopt($handler, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($handler);

if($response !==false)
{
  var_dump($response);
}
else {
  print "Could not get a response";
}

The response was

{"errorDescription":"Access token is unknown or invalid","error":"not_authorized"}
like image 145
Stanley Nguma Avatar answered Dec 10 '22 02:12

Stanley Nguma