本文整理汇总了PHP中OAuth::getRequestHeader方法的典型用法代码示例。如果您正苦于以下问题:PHP OAuth::getRequestHeader方法的具体用法?PHP OAuth::getRequestHeader怎么用?PHP OAuth::getRequestHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类OAuth
的用法示例。
在下文中一共展示了OAuth::getRequestHeader方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: OAuth
<?php
phpinfo();
/**
* Example API call
* GET profile information
*/
$consumer_key = '123456';
$consumer_secret = '56xyAzi1';
$url = 'http://api.figshare.com/v1/my_data/articles';
$method = 'POST';
$oauth = new OAuth($consumer_key, $consumer_secret);
$oauth->setToken('n4oTQ22l4FxsuQlYZlhCFwYrrSgrlPn1lhIx32uzwzAwn4oTQ22l4FxsuQlYZlhC1F', '0MNOqkQncNHuKTi6fQ8MuA');
$OA_header = $oauth->getRequestHeader($method, $url);
$headers = array("Content-Type: application/json", "Authorization: {$OA_header}");
$data = json_encode(array('title' => 'Test dataset', 'description' => 'Test description', 'defined_type' => 'dataset'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
echo $response;
示例2: testRequestAuthentication
/**
* Tests OAuth authentication in requests.
*/
function testRequestAuthentication()
{
$entity_type = 'entity_test';
$resource = 'entity:' . $entity_type;
$method = 'GET';
$format = 'json';
// Allow GET requests through OAuth on entity_test.
$config = \Drupal::configFactory()->getEditable('rest.settings');
$settings = array();
$settings[$resource][$method]['supported_formats'][] = $format;
$settings[$resource][$method]['supported_auth'][] = 'oauth';
$config->set('resources', $settings);
$config->save();
$this->container->get('router.builder')->rebuild();
// Create an entity programmatically.
$entity_values = array('name' => 'Some name', 'user_id' => 1, 'field_test_text' => array(0 => array('value' => 'Some value', 'format' => 'plain_text')));
$entity = entity_create($entity_type, $entity_values);
$entity->save();
// Create a user account that has the required permissions to read
// resources via the REST API.
$permissions = array('view test entity', 'restful get entity:' . $entity_type, 'access own consumers');
$account = $this->drupalCreateUser($permissions);
$this->drupalLogin($account);
// Generate a set of consumer keys.
$this->drupalPostForm('oauth/consumer/add/' . $account->id(), array(), 'Add');
// Get the consumer we just generated for the new user.
$user_data = \Drupal::service('user.data')->get('oauth', $account->id());
// Now send an authenticated request to read the entity through REST.
$url = $entity->urlInfo()->setRouteParameter('_format', $format);
$endpoint = $url->setAbsolute()->toString();
$oauth = new \OAuth(key($user_data), $user_data[key($user_data)]['consumer_secret']);
$oauth_header = $oauth->getRequestHeader('GET', $endpoint);
$out = $this->curlExec(array(CURLOPT_HTTPGET => TRUE, CURLOPT_NOBODY => FALSE, CURLOPT_URL => $endpoint, CURLOPT_HTTPHEADER => array('Authorization: ' . $oauth_header)));
$this->verbose('GET request to: ' . $endpoint . '<hr />' . $out);
$this->assertResponse('200', 'HTTP response code is 200 for successfully authenticated request.');
$this->curlClose();
}
示例3: UploadTweetMedia
function UploadTweetMedia($mediaUrl)
{
global $twitterCredentials;
if ($twitterCredentials === false) {
return false;
}
if (!$mediaUrl) {
return false;
}
$data = \Newsstand\HTTP::Get($mediaUrl);
if (!$data) {
return false;
}
$boundary = '';
$mimedata['media'] = "content-disposition: form-data; name=\"media\"\r\nContent-Type: image/png\r\nContent-Transfer-Encoding: binary\r\n\r\n" . $data;
while ($boundary == '') {
for ($x = 0; $x < 16; $x++) {
$boundary .= chr(rand(ord('a'), ord('z')));
}
foreach ($mimedata as $d) {
if (strpos($d, $boundary) !== false) {
$boundary = '';
}
}
}
$mime = '';
foreach ($mimedata as $d) {
$mime .= "--{$boundary}\r\n{$d}\r\n";
}
$mime .= "--{$boundary}--\r\n";
$oauth = new OAuth($twitterCredentials['consumerKey'], $twitterCredentials['consumerSecret']);
$oauth->setToken($twitterCredentials['WoWTokens']['accessToken'], $twitterCredentials['WoWTokens']['accessTokenSecret']);
$url = 'https://upload.twitter.com/1.1/media/upload.json';
$requestHeader = $oauth->getRequestHeader('POST', $url);
$inHeaders = ["Authorization: {$requestHeader}", 'Content-Type: multipart/form-data; boundary=' . $boundary];
$outHeaders = [];
$ret = \Newsstand\HTTP::Post($url, $mime, $inHeaders, $outHeaders);
if ($ret) {
$json = json_decode($ret, true);
if (json_last_error() == JSON_ERROR_NONE) {
if (isset($json['media_id_string'])) {
return $json['media_id_string'];
} else {
DebugMessage('Parsed JSON response from post to twitter, no media id', E_USER_WARNING);
DebugMessage(print_r($json, true), E_USER_WARNING);
return false;
}
} else {
DebugMessage('Non-JSON response from post to twitter', E_USER_WARNING);
DebugMessage($ret, E_USER_WARNING);
return false;
}
} else {
DebugMessage('No/bad response from post to twitter', E_USER_WARNING);
DebugMessage(print_r($outHeaders, true), E_USER_WARNING);
return false;
}
}