3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * Makes an API request to elucidat * @param $headers * @param $fields * @param $url * @param $consumer_secret * @return mixed */ function call_elucidat($headers, $fields, $method, $url, $consumer_secret){ //Build a signature $headers['oauth_signature'] = build_signature($consumer_secret, array_merge($headers, $fields), $method, $url); //Build OAuth headers $auth_headers = 'Authorization:'; $auth_headers .= build_base_string($headers, ','); //Build the request string $fields_string = build_base_string($fields, '&'); //Set the headers $header = array($auth_headers, 'Expect:'); // Create curl options if(strcasecmp($method, "GET") == 0){ $url .= '?'.$fields_string; $options = array( CURLOPT_HTTPHEADER => $header, CURLOPT_HEADER => false, CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false); } else { $options = array( CURLOPT_HTTPHEADER => $header, CURLOPT_HEADER => false, CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_POST => count($fields), CURLOPT_POSTFIELDS => $fields_string); } //Init the request and set its params $request = curl_init(); curl_setopt_array($request, $options); //Make the request $response = curl_exec($request); $status = curl_getinfo($request, CURLINFO_HTTP_CODE); curl_close($request); return array( 'status' => $status, 'response' => json_decode($response, true) ); } /** * Each request to the elucidat API must be accompanied by a unique key known as a nonce. * This key adds an additional level of security to the API. * A new key must be requested for each API call. * @param $api_url * @param $consumer_key * @param $consumer_secret * @return bool */ function get_nonce($api_url, $consumer_key, $consumer_secret){ // Start with some standard headers, unsetting the oauth_nonce. without the nonce header the API will automatically issue us one. $auth_headers = auth_headers($consumer_key); unset($auth_headers['oauth_nonce']); //Make a request to elucidat for a nonce...any url is fine providing it doesnt already have a nonce $json = call_elucidat($auth_headers, array(), 'GET', $api_url, $consumer_secret); if(isset($json['response']['nonce'])){ return $json['response']['nonce']; } return false; } /** * Computes and returns a signature for the request. * @param $secret * @param $fields * @param $request_type * @param $url * @return string */ function build_signature($secret, $fields, $request_type, $url){ ksort($fields); //Build base string to be used as a signature $base_info = $request_type.'&'.$url.'&'.build_base_string($fields, '&'); //return complete base string //Create the signature from the secret and base string $composite_key = rawurlencode($secret); return base64_encode(hash_hmac('sha1', $base_info, $composite_key, true)); } /** * Builds a segment from an array of fields. Its used to create string representations of headers and URIs * @param $fields * @param $delim * @return string */ function build_base_string($fields, $delim){ $r = array(); foreach($fields as $key=>$value){ $r[] = rawurlencode($key) . "=" . rawurlencode($value); } return implode($delim, $r); //return complete base string } /** * Returns typical headers needed for a request * @param $consumer_key * @param $nonce */ function auth_headers($consumer_key, $nonce = ''){ return array('oauth_consumer_key' => $consumer_key, 'oauth_nonce' => $nonce, 'oauth_signature_method' => 'HMAC-SHA1', 'oauth_timestamp' => time(), 'oauth_version' => '1.0'); } $nonce = get_nonce('https://api.elucidat.com/v2/projects', '1558A022-336C-6922-4B1F-AD3505839D5A', 'A393E5ED-C79F-4EB7-7DAE-9CB6C090B9E8'); $headers = auth_headers('1558A022-336C-6922-4B1F-AD3505839D5A', $nonce); $fields = array('simulation_mode'=>'simulation'); $result = call_elucidat($headers, $fields, 'GET', 'https://api.elucidat.com/v2/projects', 'A393E5ED-C79F-4EB7-7DAE-9CB6C090B9E8'); echo ("HTTP status code: " . $result['status'] . "\n"); print_r($result['response']);
Output for 7.2.0
Warning: Use of undefined constant CURLOPT_HTTPHEADER - assumed 'CURLOPT_HTTPHEADER' (this will throw an Error in a future version of PHP) in /in/sHAjj on line 26 Warning: Use of undefined constant CURLOPT_HEADER - assumed 'CURLOPT_HEADER' (this will throw an Error in a future version of PHP) in /in/sHAjj on line 27 Warning: Use of undefined constant CURLOPT_URL - assumed 'CURLOPT_URL' (this will throw an Error in a future version of PHP) in /in/sHAjj on line 28 Warning: Use of undefined constant CURLOPT_RETURNTRANSFER - assumed 'CURLOPT_RETURNTRANSFER' (this will throw an Error in a future version of PHP) in /in/sHAjj on line 29 Warning: Use of undefined constant CURLOPT_SSL_VERIFYPEER - assumed 'CURLOPT_SSL_VERIFYPEER' (this will throw an Error in a future version of PHP) in /in/sHAjj on line 30 Fatal error: Uncaught Error: Call to undefined function curl_init() in /in/sHAjj:42 Stack trace: #0 /in/sHAjj(69): call_elucidat(Array, Array, 'GET', 'https://api.elu...', 'A393E5ED-C79F-4...') #1 /in/sHAjj(122): get_nonce('https://api.elu...', '1558A022-336C-6...', 'A393E5ED-C79F-4...') #2 {main} thrown in /in/sHAjj on line 42
Process exited with code 255.
Output for 7.0.0 - 7.1.10
Notice: Use of undefined constant CURLOPT_HTTPHEADER - assumed 'CURLOPT_HTTPHEADER' in /in/sHAjj on line 26 Notice: Use of undefined constant CURLOPT_HEADER - assumed 'CURLOPT_HEADER' in /in/sHAjj on line 27 Notice: Use of undefined constant CURLOPT_URL - assumed 'CURLOPT_URL' in /in/sHAjj on line 28 Notice: Use of undefined constant CURLOPT_RETURNTRANSFER - assumed 'CURLOPT_RETURNTRANSFER' in /in/sHAjj on line 29 Notice: Use of undefined constant CURLOPT_SSL_VERIFYPEER - assumed 'CURLOPT_SSL_VERIFYPEER' in /in/sHAjj on line 30 Fatal error: Uncaught Error: Call to undefined function curl_init() in /in/sHAjj:42 Stack trace: #0 /in/sHAjj(69): call_elucidat(Array, Array, 'GET', 'https://api.elu...', 'A393E5ED-C79F-4...') #1 /in/sHAjj(122): get_nonce('https://api.elu...', '1558A022-336C-6...', 'A393E5ED-C79F-4...') #2 {main} thrown in /in/sHAjj on line 42
Process exited with code 255.
Output for 5.4.0 - 5.6.28
Notice: Use of undefined constant CURLOPT_HTTPHEADER - assumed 'CURLOPT_HTTPHEADER' in /in/sHAjj on line 26 Notice: Use of undefined constant CURLOPT_HEADER - assumed 'CURLOPT_HEADER' in /in/sHAjj on line 27 Notice: Use of undefined constant CURLOPT_URL - assumed 'CURLOPT_URL' in /in/sHAjj on line 28 Notice: Use of undefined constant CURLOPT_RETURNTRANSFER - assumed 'CURLOPT_RETURNTRANSFER' in /in/sHAjj on line 29 Notice: Use of undefined constant CURLOPT_SSL_VERIFYPEER - assumed 'CURLOPT_SSL_VERIFYPEER' in /in/sHAjj on line 30 Fatal error: Call to undefined function curl_init() in /in/sHAjj on line 42
Process exited with code 255.