简体   繁体   English

PHP curl_setopt等效于curl -d

[英]PHP curl_setopt equivalent to curl -d

I got the following curl command to work on Linux: 我得到以下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

However, when I tried to do this on PHP, the data is not being sent to the server properly, this is my set_opt commands: 但是,当我尝试在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));

What did I miss? 我错过了什么?

You use CURL options improperly. 您使用了不正确的CURL选项。 CURLOPT_PUT is intended for sending files, it is not suited for your case. CURLOPT_PUT用于发送文件,不适合您的情况。 You have to use CURLOPT_CUSTOMREQUEST option and set it to "PUT" , not "POST" . 您必须使用CURLOPT_CUSTOMREQUEST选项并将其设置为"PUT" ,而不是"POST"

So the code should look like this: 因此,代码应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM