+ Reply to Thread
Results 1 to 5 of 5

Thread: Here is a working php/curl setup example

  1. #1
    Join Date
    Aug 2015
    Posts
    3

    Default Here is a working php/curl setup example

    I wanted to use php and curl rather than the examples provided.
    Here's what I came up with as an example:

    Code:
    	# this API key has be be updated every 6 months? from https://developer.efi.com 2/2016 is next update?
    	$APIKEY = 'my/api-key/stripped';
    	$USERNAME = 'guest'; 
    	$PASSWORD = '';
    	$SERVERURL = 'https://IP_ADDRESS_OF_SERVER/live/api/v2/';
    	$cookiejar = tempnam('/tmp/', 'efiapicookie');						# will have to change path for windows
    	
    	$ch = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $SERVERURL . 'login');
    	curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar);					# reads cookies from this file, but more importantly turns on cookie engine
    	curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);					# writes cookies to this file on cleanup, probably not necessary
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);					# the Fiery uses a self-signed, non fqdn ssl cert
    	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);					# the Fiery uses a self-signed, non fqdn ssl cert
    	
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);						# allows curl_exec to send to a variable
    	curl_setopt($ch, CURLOPT_POST, true);				
    	
    	# these are what gets posted to the REST API to login - it sets a session cookie that gets used for future requests
    	$loginpostfields = 'username=' . $USERNAME . '&password=' . $PASSWORD . '&accessrights[a1]=' . urlencode($APIKEY);
    
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $loginpostfields);
    	$curlout = curl_exec($ch);
    	if(curl_errno($ch)) {
        echo 'Connection Error: ' . curl_error($ch);
        exit;
    	}
    	elseif('{"authenticated":true}' != $curlout) {
    		echo 'Authentication Error: ' . $curlout;
    		exit;
    	}
    
    	curl_setopt($ch, CURLOPT_POST, false);
    	curl_setopt($ch, CURLOPT_URL, $SERVERURL . 'consumables');
    	$curlout = curl_exec($ch);
    	$consumables = json_decode($curlout, true);
    	
    	# show list of toners
    	foreach($consumables['colorants'] as $colorants) {
    		echo $colorants['name'] . ' ' . $colorants['level'] . "% ";
    	}
    	echo "\n";
    	
    	# show tray status
    	foreach($consumables['trays'] as $trays) {
    		echo $trays['i18n'] . ' ' . $trays['level'] . "% ";
    	}
    	echo "\n";
    	
    
    	# info, gives not much useful to me?
    	curl_setopt($ch, CURLOPT_URL, $SERVERURL . 'info');
    	$curlout = curl_exec($ch);
    	echo str_replace("=>\n", '=>', print_r(json_decode($curlout, true), true));
    	/ *
    		echo str_replace("=>\n", '=>', print_r(json_decode($curlout, true), true));
    	  [disk_available] => 340029024
        [disk_total] => 419178492
        [epoch_time] => 1440695002
        [locale] => English_United States.1252
        [memory_available] => 788288
        [memory_total] => 788948
        [name] => SERVER-STRIPPED
        [serial_number] => STRIPPED
        [timezone] => Eastern Standard Time (-0400)
        [uptime] => 35561
        [version] => 1.0_SP6
        [host] => localhost
        [username] => admin
        [push_notifications] => 1
    	*/
    	
    	
    	# detail, lots of not-helpful stuff
    	curl_setopt($ch, CURLOPT_URL, $SERVERURL . 'info/detail');
    	$curlout = curl_exec($ch);	
    	echo str_replace("=>\n", '=>', print_r(json_decode($curlout, true), true));
    	/ *
    	echo str_replace("=>\n", '=>', print_r(json_decode($curlout, true), true));
    	  [DST] => 1
        [date] => 8/27/2015
        [date_format] => MM/DD/YYYY
        [default_paper_size] => US
        [disk_available] => 340028884
        [disk_total] => 419178492
        [epoch_time] => 1440695166
        [fiery_locale] => en_US
        [fiery_locales] =>  ... cut 7 locales ....
        [locale] => en_US
        [measurement_unit] => US
        [memory_available] => 788288
        [memory_total] => 788948
        [name] => SERVER-STRIPPED
        [number_format] => .
        [os_locale] => en_US
        [os_locales] => ... cut 7 locales ....
        [productid] => 1-STRIPPED
        [serial_number] => STRIPPED
        [time] => 13:6:6
        [timezone] => (UTC-05:00) Eastern Time (US & Canada)
        [timezones] =>  ... cut 101 timezones ...
        [uptime] => 35725
        [version] => 1.0_SP6
        [host] => localhost
        [username] => admin
      */
    
    	curl_setopt($ch, CURLOPT_URL, $SERVERURL . 'devices');
    	$curlout = curl_exec($ch);
    	echo str_replace("=>\n", '=>', print_r(json_decode($curlout, true), true));
    	
    	$deviceinfo = json_decode($curlout, true);
    	echo "Currently Printing: " . implode(', ', $deviceinfo[0]['jobs'][0]) . "\n";
    	echo "Status Message: " . $deviceinfo[0]['status']['status message'] . "\n";
    	echo "Warning Message: " . $deviceinfo[0]['status']['warning message'] . "\n";
    	
    	/ *
    	echo str_replace("=>\n", '=>', print_r(json_decode($curlout, true), true));
    	array(1) {
      [0]=>
      object(stdClass)#1 (6) {
        ["id"]=>    int(0)
        ["jobs"]=>    array(0) {    }
        ["name"]=>    string(10) "brunello_0"
        ["online"]=>    bool(true)
        ["prettyname"]=>    string(5) "DP700"
        ["status"]=>
        object(stdClass)#2 (5) {
          ["dup status message"]=>      string(36) "VID_ST_DRUM_CHANGE_SOON1,TONER_Y,"0""
          ["dup warning message"]=>      string(36) "VID_ST_DRUM_CHANGE_SOON1,TONER_Y,"0""
          ["lcd status"]=>      string(11) "VID_ST_IDLE"
          ["status message"]=>      string(52) "Drum Cartridge Yellow [R0] needs to be replaced soon"
          ["warning message"]=>      string(52) "Drum Cartridge Yellow [R0] needs to be replaced soon"
    	*/
    
    
    
    	# you'll need a job id	
    	curl_setopt($ch, CURLOPT_URL, $SERVERURL . 'jobs/81CBC850.55DDF3EC.8492');
    	$curlout = curl_exec($ch);
    	echo str_replace("=>\n", '=>', print_r(json_decode($curlout, true), true));
    	# Lot of info about the job, hopefully enough to submit it
    
    	# I cant get this to work yet
    	# it gives the right number of jobs, but they are all empty arrays
    	#curl_setopt($ch, CURLOPT_URL, $SERVERURL . 'jobs?key[]=held');
    	curl_setopt($ch, CURLOPT_URL, $SERVERURL . 'jobs/held');
    	$curlout = curl_exec($ch);
    	echo str_replace("=>\n", '=>', print_r(json_decode($curlout, true), true));
    
    	
    	curl_setopt($ch, CURLOPT_URL, $SERVERURL . 'cost');
    	$curlout = curl_exec($ch);
    	# LOTS OF DATA
    	echo str_replace("=>\n", '=>', print_r(json_decode($curlout, true), true));
    	
    	
    	curl_setopt($ch, CURLOPT_URL, $SERVERURL . 'queues');
    	$curlout = curl_exec($ch);
    	
    
    	echo str_replace("=>\n", '=>', print_r(json_decode($curlout, true), true));
    	
    	/ *
    	echo str_replace("=>\n", '=>', print_r(json_decode($curlout, true), true));
        [0]     [id] => 883296590
                [name] => DP700 direct
                [description] => 
                [factory] => 1
                [available] => 1
        [1]  [id] => 196150330
                [name] => DP700 hold
                [description] => 
                [factory] => 1
                [available] => 1
        [2]      [id] => 161730
                [name] => DP700 print
                [description] => 
                [factory] => 1
                [available] => 1
        [3]     [id] => 135980580
                [name] => marktest
                [description] => 
                [factory] => 
                [available] => 1
    	
    	*/
    	
    	# a specific printer
    	curl_setopt($ch, CURLOPT_URL, $SERVERURL . 'queues/135980580');
    	$curlout = curl_exec($ch);
    	echo str_replace("=>\n", '=>', print_r(json_decode($curlout, true), true));
    	# same as above, no more detail
    	
    	curl_close($ch);
    	unlink($cookiejar);
    If anyone knows how to get the held jobs, or any jobs by jobs state I'd love to know the syntax.

    I've tried a bunch of different combos, but can't get a list. I can get an empty array for every job, but no actual info.

    GET https://MY_SERVER_IP/live/api/v2/jobs?key[]=held'
    gives back these blank arrays as raw output:
    [{},{},{}]

    There's actually over 500 of the empty arrays. That's really, really close to the sum of printing+processing+held+printed+archived.

    Thanks for any help!
    Mark
    Last edited by mingles; 08-28-2015 at 07:53 AM. Reason: code didnt show everything

  2. #2
    Yusuke_K is offline Senior Fiery Forum Contributor Yusuke_K is on a distinguished path
    Join Date
    Jul 2009
    Posts
    55

    Default

    Hi Mark,

    Please try to use GET /live/api/v2/jobs/:state. For held jobs, it'll be

    GET /live/api/v2/jobs/held

    You'll be able to find more information from "jobs" section of the documentation page.
    https://developer.efi.com/documentation/api/v2

    Thank you!
    Yusuke

  3. #3
    Join Date
    Aug 2015
    Posts
    3

    Default

    Thanks for your reply. I tried that. That's what is in the code example above.
    Here's the raw HTTP get/response

    Code:
    > GET /live/api/v2/jobs/held HTTP/1.1
    Host: 192.168.x.x
    Accept: */*
    Cookie: _session_id=5c72a632d1a0665bf453ce54b01b9eff
    
    < HTTP/1.1 401 Unauthorized
    < Date: Tue, 29 Sep 2015 21:33:13 GMT
    < Server: thin 1.5.0 codename Knife
    < Content-Type: application/json; charset=utf-8
    < X-UA-Compatible: IE=Edge,chrome=1
    < Cache-Control: no-cache
    < X-Request-Id: 92b61dfea5e61f1520b8a1d5c4695db0
    < X-Runtime: 0.003906
    < Vary: Accept-Encoding
    < Transfer-Encoding: chunked
    * Connection #0 to host 192.168.x.x left intact
    AgaliVersion: 1.3.00.27
    RubyVersion: 1.9.03.146b
    RhythmVersion: 1.3.12

    Mark

  4. #4
    Yusuke_K is offline Senior Fiery Forum Contributor Yusuke_K is on a distinguished path
    Join Date
    Jul 2009
    Posts
    55

    Default

    Hi Mark,

    I'm sorry that you are still having an issue.

    The message generally means that the session might be expired or incorrect.

    Could you please trying logging in to your Fiery through Fiery API, make sure your login is active and then try GET ~/jobs/held?

    Thank you,
    Yusuke

  5. #5
    Join Date
    Aug 2015
    Posts
    3

    Default

    The request to jobs/held is between working API requests. There are no session problems. I can call /jobs and get a list of all the jobs, but jobs/held returns the 401 unauthorized on both my 700i and 1000s.

    on the 700:
    https://192.168.x.x/live/api/v2/version
    "AgaliVersion":"1.3.00.27","RubyVersion":"1.9.03.146b","RhythmVersion":"1.3.12"

    On a brand new 1000
    https://192.168.y.y/live/api/v2/version
    "AgaliVersion":"1.3.00.21","RubyVersion":"1.9.03.146b","RhythmVersion":"1.3.12"

    Mark

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts