Skip to content

Error handling and SSL fixes #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2010 Zencoder
Copyright (c) 2011 Zencoder

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
9 changes: 5 additions & 4 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Zencoder API PHP Library
==========================

Author: [Steve Heffernan](http://www.steveheffernan.com) (steve (a) zencoder (.) com)
Author: [Steve Heffernan](http://www.steveheffernan.com) (steve (a) zencoder (.) com)
Company: [Zencoder - Online Video Encoder](http://zencoder.com)
Version: 1.1
Date: 2010-06-04
Repository: <http://github.com/zencoder/zencoder-php/>
Version: 1.2
Date: 2011-08-06
Repository: <http://github.com/mchristopher/zencoder-php/>

For more details on the Zencoder API requirements visit
<http://zencoder.com/docs/api>
Expand Down Expand Up @@ -195,5 +195,6 @@ Your [notifications page](https://app.zencoder.com/notifications) will come in h

VERSIONS
---------
Version 1.2 - 2011-08-06 Added fixes for PHP Notices and SSL handling
Version 1.1 - 2010-06-04 Added General API Requests
Version 1.0 - 2010-04-02 Jobs and Notifications.
42 changes: 29 additions & 13 deletions Zencoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@
/*

Zencoder API PHP Library
Version: 1.0
Version: 1.2
See the README file for info on how to use this library.

*/
define('ZENCODER_LIBRARY_NAME', "ZencoderPHP");
define('ZENCODER_LIBRARY_VERSION', "1.0");
define('ZENCODER_LIBRARY_VERSION', "1.2");

// Add JSON functions for PHP < 5.2.0
if(!function_exists('json_encode')) {
require_once('lib/JSON.php');
require_once(dirname(__FILE__) . '/lib/JSON.php');
$GLOBALS['JSON_OBJECT'] = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
function json_encode($value) { return $GLOBALS['JSON_OBJECT']->encode($value); }
function json_decode($value) { return $GLOBALS['JSON_OBJECT']->decode($value); }
}

// Check that cURL extension is enabled
if(!function_exists('curl_init')) {
throw new Exception('You must have the cURL extension enabled to use this library.');
}

class ZencoderJob {

var $new_job_url = "https://app.zencoder.com/api/jobs";
var $new_job_url = "https://app.zencoder.com/api/v1/jobs";
var $new_job_params = array();
var $created = false;
var $errors = array();
Expand All @@ -35,12 +40,12 @@ class ZencoderJob {
function ZencoderJob($params, $options = array()) {

// Build using params if not sending request
if($options["build"]) {
if(!empty($options["build"])) {
$this->update_attributes($params);
return true;
}

if($options["url"]) $this->new_job_url = $options["url"];
if(!empty($options["url"])) $this->new_job_url = $options["url"];
$this->new_job_params = $params;
$this->created = $this->create();
}
Expand Down Expand Up @@ -75,7 +80,7 @@ function update_attributes($attributes = array()) {
// Use the Label for the key if avaiable.
function create_outputs($outputs = array()) {
foreach($outputs as $output_attrs) {
if($output_attrs["label"]) {
if(!empty($output_attrs["label"])) {
$this->outputs[$output_attrs["label"]] = new ZencoderOutputFile($output_attrs);
} else {
$this->outputs[] = new ZencoderOutputFile($output_attrs);
Expand Down Expand Up @@ -172,10 +177,9 @@ class ZencoderCURL {
CURLOPT_HEADER => 0, // Don't return the header in result
CURLOPT_HTTPHEADER => array("Content-Type: application/json", "Accept: application/json"),
CURLOPT_CONNECTTIMEOUT => 0, // Time in seconds to timeout send request. 0 is no timeout.
// CURLOPT_FOLLOWLOCATION => 0, // Follow redirects. (stopped because it was causing an error in safe mode)
// Dealing with the certificate. Still a sketchy area.
CURLOPT_SSL_VERIFYPEER => 0, // Turn off verification, curl -k or --insecure
CURLOPT_SSL_VERIFYHOST => 0
CURLOPT_FOLLOWLOCATION => 1, // Follow redirects.
CURLOPT_SSL_VERIFYPEER => 1,
CURLOPT_SSL_VERIFYHOST => 1
);

var $connected;
Expand All @@ -186,6 +190,11 @@ class ZencoderCURL {
// Initialize
function ZencoderCURL($url, $json, $options = array()) {

// If PHP in safe mode, disable following location
if( ini_get('safe_mode') ) {
$this->options[CURLOPT_FOLLOWLOCATION] = 0;
}

// Add library details to request
$this->options[CURLOPT_HTTPHEADER][] = "Zencoder-Library-Name: ".ZENCODER_LIBRARY_NAME;
$this->options[CURLOPT_HTTPHEADER][] = "Zencoder-Library-Version: ".ZENCODER_LIBRARY_VERSION;
Expand All @@ -209,6 +218,13 @@ function ZencoderCURL($url, $json, $options = array()) {

// Execute session and store returned results
$this->results = curl_exec($ch);

// Code based on Facebook PHP SDK
// Retries request if unable to validate cert chain
if (curl_errno($ch) == 60) { // CURLE_SSL_CACERT
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/lib/zen_ca_chain.crt');
$this->results = curl_exec($ch);
}

// Store the HTTP status code given (201, 404, etc.)
$this->status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Expand All @@ -233,8 +249,8 @@ class ZencoderOutputNotification {
var $job;

function ZencoderOutputNotification($params) {
if($params["output"]) $this->output = new ZencoderOutputFile($params["output"]);
if($params["job"]) $this->job = new ZencoderJob($params["job"], array("build" => true));
if(!empty($params["output"])) $this->output = new ZencoderOutputFile($params["output"]);
if(!empty($params["job"])) $this->job = new ZencoderJob($params["job"], array("build" => true));
}

function catch_and_parse() {
Expand Down
51 changes: 51 additions & 0 deletions lib/zen_ca_chain.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
-----BEGIN CERTIFICATE-----
MIIEIDCCAwigAwIBAgIQNE7VVyDV7exJ9C/ON9srbTANBgkqhkiG9w0BAQUFADCB
qTELMAkGA1UEBhMCVVMxFTATBgNVBAoTDHRoYXd0ZSwgSW5jLjEoMCYGA1UECxMf
Q2VydGlmaWNhdGlvbi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