You are hereForums / JDF Generally / Common JDF Questions / Does anyone succesfully made a JMF HTTP script in PHP?

Does anyone succesfully made a JMF HTTP script in PHP?


14 replies [Last post]
Offline
Joined: 01/18/2012

Hi,

I´m building a MIS system to interact with esko backtage. I´ve managed to make connection and send JMF with Alces.
Today I use hotfolders for JDF sending but as it only checks the hotfolder every 1 minute it is very timeconsuming.

But now I want to make my PHP script to send the JMF/JDF. Has anyone sucessfully done this or has any tips.
I´m trying to do it using cURL and POST but dont seem to get any response from the server.

I can´t find any information about the HTTP connection on cip4.

Please help me

/Pontus

Offline
Joined: 06/24/2011
PHP service for JMF

Hi Pontus,

 

I've done a service in PHP which sends and receives JMF without any problems.

Can you tell me what you've done so far and where exactly your problem is?

 

Regards,

 

Albert van Peppen

 

Offline
Joined: 01/18/2012
PHP service for JMF

Hi Albert

The problem is that I have no idea how to communicate over HTTP with JMF. I´m trying to do it with cURL.

Like this:

function createJMF()
{
    
    $eskoJMF="http://rstbs1:4411/JDFP/JMF";
    $urlJDF="http://rstintranet/php/jmfHF/TESTINGET.jdf";
    
    $xml1='
    <JMF xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.CIP4.org/JDFSchema_1_1" SenderID="Alces"
        TimeStamp="2004-08-30T17:23:00+01:00" Version="1.2">
        <Command ID="C1" Type="OpenQueue" xsi:type="CommandOpenQueue" />
    </JMF>';
    
    // init curl handle
    $ch = curl_init($eskoJMF);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); // follow redirects recursively
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml1);

    // perform post
    echo $pnp_result_page = curl_exec($ch);
    curl_close ($ch); 
}

createJMF();

This example is just a test to get the connection working. But I cant seem to get a response from the server whatever I try.

Regards
/Pontus Axelsson

Offline
Joined: 06/24/2011
HTTP
I don't use Curl; I use the http functions. I will provide you with a sample somewhere tonight or tomorow (running out if time here :( ) Albert
Offline
Joined: 06/24/2011
Sample code in PHP

Ok, I've tried to put a sample together (our PHP apps use a set of classes which handles everything, but it should end up like this :) )

<?php
 //
 // Sample application sending JMF to a JMF handling service
 //

 // For our JDF stuff this must be turned off! (Should be turned off in the PHP.INI but now we know for sure)
 set_magic_quotes_runtime(0);


 // HTTP result status to caller
 $lResultStatus = HTTP_STATUS_OK;    // HTTP_STATUS_OK: Assume all is OK
 $strResultStatusString = 'OK';
 $strContentType = 'application/vnd.cip4-jmf+xml';

 // Build XML data containing the JMF
 $strXML = '<?xml version="1.0" encoding="UTF-8" ?>'."\n".
     '<JMF xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'.
     '    xmlns="http://www.CIP4.org/JDFSchema_1_1" SenderID="Alces"'.
     '    TimeStamp="2004-08-30T17:23:00+01:00" Version="1.2">'.
     '   <Command ID="C1" Type="OpenQueue" xsi:type="CommandOpenQueue" />'.
     '</JMF>'."\n";


 $strURL = 'http://jmfserver/jmfservice';

 $http_host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
 $http_server_referer = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : '';

 // Set data
 if (!empty($http_host)) {
     $arrHeaders['Host'] = htmlentities($http_host, ENT_QUOTES, "ISO-8859-15");
 }

 if (!empty($http_server_referer)) {
     $arrHeaders['Referer'] = $http_server_referer;
 }

 $arrHeaders['Connection'] = 'Close'."\n";   // Keep-Alive if you want to do more communication like taking results :)
 $arrHeaders['Content-Type'] = $strContentType."\n";
 $arrHeaders['Content-Length'] = strval(strlen($strXML))."\n";
 $arrHeaders['Expect'] = '';
 $arrHeaders['Accept'] = '';

 // Add the MIME stuff etc in this senddata as well
 $strSendData = $strXML;

 // HttpRequest
 try {
     $cReq = new HttpRequest($strURL, HttpRequest::METH_POST, Array("useragent" => "MyJMFProvider 1.0",
          "referer" => $http_server_referer,
          "headers" => $arrHeaders));
     $cReq->setRawPostData($strSendData);
     $res = $cReq->send();

     // Get HTTP response code (should be 0 = OK)
     $lHttpResult = $res->getResponseCode();

     // Get response message (Most likely 'OK')
     $strResponseMessage = $cReq->getResponseMessage();

     // Do some result checking here
     // ....
 }
 catch (HttpException $ex) {
     echo 'Send HTTP request exception: '.$ex;
     die();
 }

 // Flush caches
 flush();

?>

It might not work directly, but the basics are clear I assume.

 

Regards,

 

Albert van Peppen

 

Offline
Joined: 01/18/2012
Thanks

Hi Albert,

Thanks, I will give it a try right away.

Offline
Joined: 06/24/2011
And?
And? Any luck?
Offline
Joined: 01/18/2012
no luck

Whatever I try I get a 500 error. I got a error installing pecl on linux. Seems to be a bug in pecl.

Offline
Joined: 06/24/2011
No luck?
Did you enable logging in your PHP.INI? And looked in the logfile? Most likely the http or curl extensions are not loaded, but otherwise it will tell you what else you did wrong (invalid var, forgot a semicol etc :) ) I'm on Windows and have no Linux machine (with PHP installed) at hand right now to help you out there...
Offline
Joined: 01/18/2012
New PHP

curl works fine on other urls. But it seems I cant get any response from the server. Updated the curl with your script:

// $url="http://rstintranet/php/test.php";
$url="http://rstbs1:4411/JDFP/JMF";
$urlJDF="http://rstintranet/php/jmfHF/TESTINGET.jdf";
$strContentType = 'application/vnd.cip4-jmf+xml';

//$strContentType = "application/x-www-form-urlencoded"; //text/html; charset=utf-8 //application/x-www-form-urlencoded;
    
$strXML = '<?xml version="1.0" encoding="UTF-8"?><JMF xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.CIP4.org/JDFSchema_1_1" SenderID="Alces" TimeStamp="2004-08-30T17:23:00+01:00" Version="1.2"><Command ID="C1" Type="OpenQueue" xsi:type="CommandOpenQueue" /></JMF>';
    
// init curl handle
$ch = curl_init();
    
$urldata = parse_url($url);

//  Ensure we have the query too, if there is any...
$urldata['path'] .= "?".$urldata['query'];

//  Header vars
$host="Host: ".$urldata['host'];
$referer="Referer: rstintranet";
$connection="Connection: 'Keep-alive: 300'";
$contentType="Content-Type: ".$strContentType;
$contentLength="Content-Length: ".strval(strlen($strXML));
$expect="Expect: ";
$accept="Accept: ";
    
$headers = array($host, $referer, $connection, $contentType, $contentLength, $expect, $accept);
curl_setopt($ch, CURLOPT_INTERFACE, "192.168.1.93");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, @$strXML);

// perform post
$result= curl_exec($ch);
curl_close ($ch); 
    
echo ($result);
Offline
Joined: 06/24/2011
Sorry

Sorry but I don't now about Curl.

I've tried to install it on my Windows machine but PHP keeps reporting it can't find the php_curl.dll.

So I can't help you with that right now..

But what I saw in other examples curl_setopt($ch, CURLOPT_POSTFIELDS, $strXML); (without the @) should work..

The $strXML is rawdata so in princople it should not be put in the postfields; but then again, what do I know about Curl ;)

Again, check the message in your php logfile and you might check the http server logs on the machine to which you send your jmf.

Is your message arriving there? Is it complete (eg. has it everything in it you send to it)

Albert

Offline
Joined: 06/24/2011
Still no luck?
And? Did you get it to work?
Can you tell us what/how you did it?
(So aybody else who needs it can find it on the web :) )

Albert
Offline
Joined: 01/18/2012
Finally working

Finnaly got it working, the problem was cross servers. I had to fool the HTTP that it was from the same server: "curl_setopt($ch, CURLOPT_INTERFACE, "xxx.xxx.xxx.93")".
Here is the PHP script that I call with AJAX.

<?php
$strJMF=$_POST['strJMF'];

function sendit($strJMF)
{
    $url="http://xxx.xxx.xxx.91:4411/JDFP/JMF";
    $strContentType = 'application/vnd.cip4-jmf+xml';
    
    // init curl handle
    $ch = curl_init();
    
    $urldata = parse_url($url);

    $host="Host: ".$urldata['host'];
    $referer="Referer: rstintranet";
    $connection="Connection: keep-alive, Keep-alive: 300'";
    $contentType="Content-Type: ".$strContentType;
    $contentLength="Content-Length: ".strval(strlen($strJMF));
    $expect="Expect: ";
    $accept="Accept: ";
    $headers = array($host, $referer, $contentType, $contentLength);
    curl_setopt($ch, CURLOPT_INTERFACE, "xxx.xxx.xxx.93");
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $strJMF);

    // perform post
    $result= curl_exec($ch);
    curl_close ($ch); 
    return $result;
}
    $result=sendit($strJMF);
    echo $result;
?>
Offline
Joined: 06/24/2011
By the way
Be carefull when building a JDF/JMF handler in PHP; If you have a situation with for example 5 printers, each sending realtime data to you, the number of messages returning to you can run up to over 1000 per second (depending on what messages you are registered to and what messages the machines can send) In this case your PHP code should run on a high end machine with PHP set up in the most optimized way. Even then you will be missing messages (or when caching messages, you will run out of memory/handles) Just an advice :) Albert
Offline
Joined: 11/02/2010
HTTP Stream

Hi Pontus,

you have to put the message into a HTTP Stream for transmission. I'm not very familiar with PHP but I haven't find any part in your sample code which sould shows you are using a HTTP Stream.

(c)
Stefan