function sendOAuthBodyPOST($method, $endpoint, $oauth_consumer_key, $oauth_consumer_secret, $content_type, $body) { $hash = base64_encode(sha1($body, TRUE)); $parms = array('oauth_body_hash' => $hash); $test_token = ''; $hmac_method = new OAuthSignatureMethod_HMAC_SHA1(); $test_consumer = new OAuthConsumer($oauth_consumer_key, $oauth_consumer_secret, NULL); $acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, $method, $endpoint, $parms); $acc_req->sign_request($hmac_method, $test_consumer, $test_token); // Pass this back up "out of band" for debugging global $LastOAuthBodyBaseString; $LastOAuthBodyBaseString = $acc_req->get_signature_base_string(); // echo($LastOAuthBodyBaseString."\n"); $header = $acc_req->to_header(); $header = $header . "\r\nContent-Type: " . $content_type . "\r\n"; $response = post_socket_xml($endpoint, $body, $header); if ($response !== false && strlen($response) > 0) { return $response; } $params = array('http' => array('method' => 'POST', 'content' => $body, 'header' => $header)); $ctx = stream_context_create($params); try { $fp = @fopen($endpoint, 'r', false, $ctx); } catch (Exception $e) { $fp = false; } if ($fp) { $response = @stream_get_contents($fp); } else { // Try CURL $headers = explode("\r\n", $header); $response = sendXmlOverPost($endpoint, $body, $headers); } if ($response === false) { throw new Exception("Problem reading data from {$endpoint}, {$php_errormsg}"); } return $response; }
function sendOAuthBodyPOST($method, $endpoint, $oauth_consumer_key, $oauth_consumer_secret, $content_type, $body) { $hash = base64_encode(sha1($body, TRUE)); $parms = array('oauth_body_hash' => $hash); $test_token = ''; $hmac_method = new OAuthSignatureMethod_HMAC_SHA1(); $test_consumer = new OAuthConsumer($oauth_consumer_key, $oauth_consumer_secret, NULL, 11); $acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, $method, $endpoint, $parms); $acc_req->sign_request($hmac_method, $test_consumer, $test_token); // Pass this back up "out of band" for debugging global $LastOAuthBodyBaseString; $LastOAuthBodyBaseString = $acc_req->get_signature_base_string(); // echo($LastOAuthBodyBaseString."\n"); $header = $acc_req->to_header(); $header = $header . "\r\nContent-Type: " . $content_type . "\r\n"; $disabled = explode(', ', ini_get('disable_functions')); if (function_exists('exec') && !in_array('exec', $disabled)) { try { $cmd = "curl -X POST"; $headers = explode("\r\n", $header); foreach ($headers as $hdr) { if (strlen($hdr) < 2) { continue; } $cmd .= " -H '{$hdr}'"; } $cmd .= " -d '" . str_replace("'", "\\'", $body) . "' " . "'" . $endpoint . "'"; $cmd .= " > /dev/null 2>&1 &"; @exec($cmd, $output, $exit); return $exit == 0; } catch (Exception $e) { //continue below } } $response = post_socket_xml($endpoint, $body, $header); if ($response !== false && strlen($response) > 0) { return $response; } $params = array('http' => array('method' => 'POST', 'content' => $body, 'header' => $header)); $ctx = stream_context_create($params); try { $fp = @fopen($endpoint, 'r', false, $ctx); } catch (Exception $e) { $fp = false; } if ($fp) { $response = @stream_get_contents($fp); } else { // Try CURL $headers = explode("\r\n", $header); $response = sendXmlOverPost($endpoint, $body, $headers); } if ($response === false) { global $sessiondata; if ($sessiondata['debugmode'] == true) { throw new Exception("Problem reading data from {$endpoint}, {$php_errormsg}"); } else { //echo "Unable to update score via LTI."; } } return $response; }