Created
June 12, 2012 23:58
-
-
Save apanzerj/2920899 to your computer and use it in GitHub Desktop.
curlWrap function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define("ZDAPIKEY", ""); | |
define("ZDUSER", ""); | |
define("ZDURL", "https://subdomain.zendesk.com/api/v2"); | |
/* Note: do not put a trailing slash at the end of v2 */ | |
function curlWrap($url, $json, $action) | |
{ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 ); | |
curl_setopt($ch, CURLOPT_URL, ZDURL.$url); | |
curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY); | |
switch($action){ | |
case "POST": | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $json); | |
break; | |
case "GET": | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); | |
break; | |
case "PUT": | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $json); | |
break; | |
case "DELETE": | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); | |
break; | |
default: | |
break; | |
} | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); | |
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 10); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
$decoded = json_decode($output); | |
return $decoded; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment