我得到以下curl命令在Linux上工作:
curl -H "Content-Type:application/json" -H "Accept:application/json" -H "Authorization: Basic dGVsZXVuZzpuYWcweWEyMw==" -X PUT -d '{"requireJiraIssue": true, "requireMatchingAuthorEmail": "true"}' http://stash/rest/api/1.0/projects/TSD/repos/git-flow-release-test/settings/hooks/com.isroot.stash.plugin.yacc%3AyaccHook/enabled
但是,当我尝试在PHP上执行此操作时,数据未正确发送到服务器,这是我的set_opt命令:
$myURL = "http://stash/rest/api/1.0/projects/TSD/repos/git-flow-release-test/settings/hooks/com.isroot.stash.plugin.yacc:yaccHook/enabled";
$hookdata_yacc = array(
'requireJiraIssue' => true,
'requireMatchingAuthorEmail' => true
);
$data = json_encode($hookdata_yacc);
$headers = array(
"Content-Type: application/json",
"Accept: application/json",
"Authorization: Basic dGVmyPasswordEyMw==",
"Content-Length: " . strlen($data)
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
return (curl_exec($curl));
我错过了什么?
答案 0 :(得分:2)
您不正确地使用CURL选项。 CURLOPT_PUT
用于发送文件,不适合您的情况。您必须使用CURLOPT_CUSTOMREQUEST
选项并将其设置为"PUT"
,而不是"POST"
。
所以代码应如下所示:
$myURL = "http://stash/rest/api/1.0/projects/TSD/repos/git-flow-release-test/settings/hooks/com.isroot.stash.plugin.yacc:yaccHook/enabled";
$hookdata_yacc = array(
'requireJiraIssue' => true,
'requireMatchingAuthorEmail' => true
);
$data = json_encode($hookdata_yacc);
$headers = array(
"Content-Type: application/json",
"Accept: application/json",
"Authorization: Basic dGVmyPasswordEyMw==",
"Content-Length: " . strlen($data)
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $myURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
return (curl_exec($curl));