Janith Chinthana
Janith Chinthana

Reputation: 3844

Convert curl command to PHP code

I need to delete opencast matterhorn recording via REST api which they already provided. (just for the information, no need to worry about matterhorn)

I need to develop simple PHP code to DELETE some entries via given REST API. I have tested with curl command line it is working fine, but I can't convert that into working PHP code.

working curl command :

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

not working PHP command :

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

result $httpCode is 302, means it's not working.

Any idea where I went wrong.

Thanks in advance

Upvotes: 0

Views: 514

Answers (1)

dave
dave

Reputation: 64657

You need to combine these two so you aren't overwriting yourself:

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

So that should be

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

And it probably would also help to follow redirects with

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

Upvotes: 1

Related Questions