PHP curl_setopt等同于curl -d [英] PHP curl_setopt equivalent to curl -d

查看:290
本文介绍了PHP curl_setopt等同于curl -d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下curl命令在Linux上工作:

I got the following curl command to work on 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命令:

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:

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

我错过了什么?

推荐答案

您不正确地使用CURL选项。 CURLOPT_PUT 用于发送文件,它不适合您的情况。您必须使用 CURLOPT_CUSTOMREQUEST 选项并将其设置为PUT,而不是POST

You use CURL options improperly. CURLOPT_PUT is intended for sending files, it is not suit for your case. You have to use CURLOPT_CUSTOMREQUEST option and set it to "PUT", not "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));

这篇关于PHP curl_setopt等同于curl -d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆