requseting Opencast API json data using php not working!!!!

301 views
Skip to first unread message

Ahmed Al Hajri

unread,
Aug 25, 2016, 8:05:18 AM8/25/16
to Opencast Users
Hi guys,


I'm trying for many days now to request API json data using php, however it dosen't work. I even tried with online test server same issue nothing comes out of it. But, when I use other services php can request data perfectly this is my code:

<?php

$type = header("Content-Type: application/json");
$jsonUrl = 'http://localhost:8080/archive/episode.json';
$data = file_get_contents($jsonUrl);
$array = json_decode($data, true);

print_r($array);

?>

does it require authentication? and that might be the reason why it doesn't work? 

can anyone please tell me why this happens?

best wishes

Jan Koppe

unread,
Aug 25, 2016, 8:33:06 AM8/25/16
to us...@opencast.org

Hi Ahmed,

yes, Opencast uses Digest Authentication. Not sure how you would use this with php, but here are some details on how it works: https://opencast.jira.com/wiki/display/MH/Remote+Services

regards

--
You received this message because you are subscribed to the Google Groups "Opencast Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to users+un...@opencast.org.

Andberg, Sami K

unread,
Aug 25, 2016, 9:28:46 AM8/25/16
to us...@opencast.org
Hi Ahmed,

If you try using search API instead of archive and have any public videos (viewable to all in ACL) you should get answers without authenticating.

BR,
Sami

Sent using OWA for iPhone
From: Ahmed Al Hajri <a.ha...@gmail.com>
Sent: Thursday, August 25, 2016 3:05:18 PM
To: Opencast Users
Subject: [OC Users] requseting Opencast API json data using php not working!!!!
 

Per Pascal Grube

unread,
Aug 25, 2016, 2:10:03 PM8/25/16
to us...@opencast.org
Hi Ahmed,

you can have a look at [1]. There I use php to access the opencast API. I use
the php curl extension.

Cheers,

Pascal

[1] https://github.com/TIK-NFL/ilias-oc-plugin/blob/master/classes/class.ilObjMatterhorn.php
--
Per Pascal Grube
IZUS/TIK
Universitaet Stuttgart Tel: ++49-711-685-60011
Allmandring 30a
70550 Stuttgart www.tik.uni-stuttgart.de


***********************************************************

Ahmed Al Hajri

unread,
Aug 26, 2016, 10:28:42 AM8/26/16
to Opencast Users
thank you Jan,

I used this:

curl --digest -u admin:opencast -H "X-Requested-Auth: Digest" -H "X-Opencast-Matterhorn-Authorization: true" -H
-d "state=capturing"  http://demo.opencast.org:8080/archive/episode.json



but get I'm getting incorrect response:

Do you have any clue what could be the issue in this case? Is there any of configuration I need to adjust?
I tired with my localhost server and Opencast online demo server, same issue, Incorrect response.
my opencast version is 2.2.0

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 401 Incorrect response</title>
</head>
<body><h2>HTTP ERROR 401</h2>
<p>Problem accessing /archive/episode.json. Reason:
<pre>    Incorrect response</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               
<br/>                                               

</body>
</html>

Ahmed Al Hajri

unread,
Aug 26, 2016, 10:52:08 AM8/26/16
to Opencast Users
greetings thank you for your reply.

is there any documentation on how to use this plugin ? a bit confused

Christian Greweling

unread,
Aug 26, 2016, 11:02:06 AM8/26/16
to Opencast Users
Hi Ahmed,

the digest auth Credentials are opencast_system_account:CHANGE_ME on the demo server.
curl --digest -u opencast_system_account:CHANGE_ME -H "X-Requested-Auth: Digest" -H "X-Opencast-Matterhorn-Authorization: true" -H
 http://demo.opencast.org:8080/archive/episode.json

Christian

Ahmed Al Hajri

unread,
Aug 26, 2016, 11:38:22 AM8/26/16
to Opencast Users
thank you Christian,

I tired that but with out a password I'm not sure what would be the default password or from where i can get it (if you know please let know) for opencast_system_account

regards

Lars Kiesow

unread,
Aug 26, 2016, 11:40:16 AM8/26/16
to us...@opencast.org
Hi Ahmed,
you can also omit some unnecessary headers. This should suffice:

curl --digest -u opencast_system_account:CHANGE_ME \
-H "X-Requested-Auth: Digest" \
http://demo.opencast.org:8080/archive/episode.json

And note that you are talking to the archive. You are not getting the
published media which is on the search service.

Regards,
Lars

Lars Kiesow

unread,
Aug 26, 2016, 11:42:37 AM8/26/16
to us...@opencast.org
Hi Ahmed,
the default is “CHANGE_ME” :-D
It's defined in the custom.properties configuration file.
Regards,
Lars

Ahmed Al Hajri

unread,
Aug 26, 2016, 1:13:35 PM8/26/16
to Opencast Users
thanks guys it works like a ninja now on the terminal and Postman tool, BUT I want to use it on php page using cUrl, everything works fine I guess but the it takes me to the Opencast login page here my code:

error_reporting(E_ALL);
ini_set( 'display_errors','1');

$url = "http://localhost:8080/archive/episode.json";
$username = "opencast_system_account";
$password = "CHANGE_ME";

$options = array(
        CURLOPT_URL            => $url,
        CURLOPT_HEADER         => true,   
        CURLOPT_VERBOSE        => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_SSL_VERIFYPEER => false,    // for https
        CURLOPT_USERPWD        => $username . ":" . $password,
        CURLOPT_HTTPAUTH       => CURLAUTH_DIGEST,
        CURLOPT_CUSTOMREQUEST => 'GET'
);

$ch = curl_init();

curl_setopt_array( $ch, $options );

try {
  $raw_response  = curl_exec( $ch );

  // validate CURL status
  if(curl_errno($ch))
      throw new Exception(curl_error($ch), 500);

  // validate HTTP status code (user/password credential issues)
  $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  if ($status_code != 200)
      throw new Exception("Response with Status Code [" . $status_code . "].", 500);

} catch(Exception $ex) {
    if ($ch != null) curl_close($ch);
    throw new Exception($ex);
}

if ($ch != null) curl_close($ch);

echo "raw response: " . $raw_response; 


I REALLY appreciate you kind assistance

best wishes

Lars Kiesow

unread,
Aug 27, 2016, 7:21:28 AM8/27/16
to us...@opencast.org
Hi Ahmed,
you omitted one important header in your code:

X-Requested-Auth: Digest

Using C and libcurl you would do something like this:

struct curl_slist *slist1;
slist1 = curl_slist_append(slist1, "X-Requested-Auth: Digest");
...
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist1);
...

Also, I hope you know what you are doing here. Usually it is not a good
idea to do this:

CURLOPT_SSL_VERIFYPEER => false,

Regards,
Lars


On Fri, 26 Aug 2016 10:13:35 -0700 (PDT)
Ahmed Al Hajri <a.ha...@gmail.com> wrote:

Ahmed Al Hajri

unread,
Aug 27, 2016, 2:18:29 PM8/27/16
to Opencast Users
Hey thanks for the tip

it works, but i have an issue I get 2 responses can't figure out how get only JSON data here's my code:

<?php


error_reporting(E_ALL);
ini_set( 'display_errors','1');

$url = "http://localhost:8080/archive/episode.json";
$username = "opencast_system_account";
$password = "CHANGE_ME";

$options = array(
        CURLOPT_URL            => $url,
        CURLOPT_HEADER         => true,   
        CURLOPT_VERBOSE        => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_SSL_VERIFYPEER => true,    // for https

        CURLOPT_USERPWD        => $username . ":" . $password,
        CURLOPT_HTTPAUTH       => CURLAUTH_DIGEST,
        CURLOPT_CUSTOMREQUEST => 'GET'
);



$ch = curl_init();

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

curl_setopt_array( $ch, $options);


try {
  $raw_response  = curl_exec( $ch );
 

 // validate CURL status
  if(curl_errno($ch))
      throw new Exception(curl_error($ch), 500);

  // validate HTTP status code (user/password credential issues)
  $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  if ($status_code != 200)
      throw new Exception("Response with Status Code [" . $status_code . "].", 500);

} catch(Exception $ex) {
    if ($ch != null) curl_close($ch);
    throw new Exception($ex);
}

if ($ch != null) curl_close($ch);
echo "<pre>";

echo  "raw response: " . $raw_response;
echo "</pre>";

$json = json_decode($raw_response, true);

print_r($json);

           
?>

and this is the output:

raw response: HTTP/1.1 401 An Authentication object was not found in the SecurityContext
Set-Cookie: JSESSIONID=efs1sy99nwtaykf79qi097d8;Path=/
WWW-Authenticate: Digest realm="Opencast", qop="auth", nonce="MTQ3MjMyMTQ5NjAzODo1ZGNlNzdkNmU4ZDE4MzVjOTk2MTcwNTFjNGJkNTNhZg=="
Content-Type: text/html;charset=ISO-8859-1
Cache-Control: must-revalidate,no-cache,no-store
Content-Length: 1489
Server: Jetty(8.1.17.v20150415)

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=tqursqs0kfu5116h59kotili7;Path=/
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json
Date: Sat, 27 Aug 2016 18:06:36 GMT
Transfer-Encoding: chunked
Server: Jetty(8.1.17.v20150415)

{"search-results":{"offset":"0","limit":"11","total":"11","searchTime":"1","query":"oc_deleted:false AND oc_latest_version:true"..............>>>>


You can see at the bottom that I can get the data, I just want to get JSON.


from the php log file I can see that, request is sent then I get a message that "Authentication object was not found in the SecurityContext". at this point what
it dose is it set the cookie with the following "Set-Cookie: JSESSIONID=efs1sy99nwtaykf79qi097d8;Path=/ WWW-Authenticate: Digest realm="Opencast", qop="auth", nonce="MTQ3MjMyMTQ5NjAzODo1ZGNlNzdkNmU4ZDE4MzVjOTk2MTcwNTFjNGJkNTNhZg=="". then another request is sent to get
json data.

if you know how you can go directly and get json data please help me.

best wishes

and thanks againg

Karen Dolan

unread,
Aug 29, 2016, 8:31:34 AM8/29/16
to us...@opencast.org
Hello Ahmen,

Try it with  curlopt_header set to false.

Karen Dolan
Harvard University DCE



Lars Kiesow

unread,
Aug 29, 2016, 8:35:12 AM8/29/16
to us...@opencast.org
Hi,
please look up the PHP cURL manual. It is all documented in there. This
time you need to get the components from your response like this:

$result['header']
$result['body']
$result['http_code']

Regards,
Lars
> JSESSIONID=efs1sy99nwtaykf79qi097d8;Path=/WWW-Authenticate: Digest

Ahmed Al Hajri

unread,
Aug 29, 2016, 9:33:47 AM8/29/16
to Opencast Users, kdo...@dce.harvard.edu
thanks BIG time

Ahmed Al Hajri

unread,
Aug 29, 2016, 9:54:48 AM8/29/16
to Opencast Users
To everyone THANKS A LOT

now it works great

I'm glad to communicate with you

On Thursday, 25 August 2016 13:05:18 UTC+1, Ahmed Al Hajri wrote:
Reply all
Reply to author
Forward
0 new messages