如何在PHP

时间:2016-06-04 19:10:03

标签: php curl

我试图找出如何创建PHP cURL POST请求。我有以下代码可供使用。我有点卡在-g -X和-H的意思上。

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'

非常感谢任何帮助。

编辑添加了以下代码:

$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);

通过curl -h我得到了以下信息:

-g, - globoff使用{}和[]

禁用网址序列和范围

-X, - request COMMAND指定要使用的请求命令

-H, - header LINE将自定义标题LINE传递给服务器(H)

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

但我无法弄清楚如何添加这些属性。

2 个答案:

答案 0 :(得分:2)

问题可能在于您格式化$postData数组中的值的方式。 包括JSON文本在内的所有值都应使用单引号。 看看我是如何做到的,虽然响应是一个错误,我认为是一个有效的错误

$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";
}

回应是

{"errorDescription":"Access token is unknown or invalid","error":"not_authorized"}

答案 1 :(得分:-3)

Curl -h应该帮助你发现每个选项意味着什么。正如其他人所建议的那样,您可以阅读卷曲手册以查看所有卷曲选项/功能