将curl命令转换为PHP代码

时间:2014-02-04 05:51:59

标签: php rest curl

我需要通过他们已经提供的REST api删除opencast的问号。 (仅供参考,无需担心马特洪峰)

我需要通过给定的REST API为DELETE一些条目开发简单的PHP代码。我已经使用curl命令行测试它工作正常,但我无法将其转换为可用的PHP代码。

working curl命令:

curl --digest -X "DELETE" -u matterhorn_system_account:CHANGE_ME -H "X-Requested-Auth: Digest" -H "X-Opencast-Matterhorn-Authorization: true" url/search/xxxx

没有使用PHP命令:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'url/search/xxxx');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, 'matterhorn_system_account:CHANGE_ME');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-Auth: Digest"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Opencast-Matterhorn-Authorization: true"));
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

结果$httpCode为302,表示它无效。

知道我哪里出错了。

提前致谢

1 个答案:

答案 0 :(得分:1)

你需要将这两者结合起来,这样你就不会覆盖自己:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-Auth: Digest"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Opencast-Matterhorn-Authorization: true"));

那应该是

curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-Auth: Digest", 
                                           "X-Opencast-Matterhorn-Authorization: true"));

它可能也有助于跟随重定向

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);