【问题标题】:Twitter Digit API Not work CURL phpTwitter 数字 API 不起作用 CURL php
【发布时间】:2016-01-19 22:26:01
【问题描述】:

我正在使用 twitter 数字。我的移动验证正常工作。但是当我调用 curl 时使用 auth 标头信息然后它不起作用。我的 curl 代码

$request=$_REQUEST['someData'];


function oauth_value($headersIndex = [])
{
    if(empty($headersIndex)) return 'Invalid Index Number';

    $value = explode('=', $headersIndex);
    return str_replace('"', '', $value[1] );
}

$headersData = explode(',',$request['headers']);

$ch = curl_init();


$ch = curl_init($request['apiUrl']);

curl_setopt($ch, CURLOPT_HEADER, 1);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization' => 'OAuth oauth_consumer_key="'.oauth_value($headersData[0]).'",
     oauth_nonce="'.oauth_value($headersData[1]).'",
      oauth_signature="'.oauth_value($headersData[2]).'",
       oauth_signature_method="HMAC-SHA1", 
       oauth_timestamp="'.oauth_value($headersData[4]).'", 
       oauth_token="'.oauth_value($headersData[5]).'",
       oauth_version="'.oauth_value($headersData[6]).'"'
  )
);

$resp = curl_exec($ch);

$info = curl_getinfo($ch);
curl_close($ch);

来自 $_REQUEST['someData']; 的示例信息;

Array
(
    [apiUrl] => https://api.digits.com/1.1/sdk/account.json
    [headers] =>
     OAuth oauth_consumer_key="OybCXYoYTuS0Cw0usZYry6Nlj", 
    oauth_nonce="3942830480-TILitVHHZGmcczuuFj3nbJtnMm00DHvvgduawMHOybCXYoYTuS0Cw0usZYry6Nlj1445246231940",
     oauth_signature="dXHcH%2FsLBIlYOVWBIhEBWSCMLJo%3D",
      oauth_signature_method="HMAC-SHA1",
     oauth_timestamp="1445246231", 
     oauth_token="3942830480-TILitVHHZGmcczuuFj3nbJtnMm00DHvvgduawMH",
      oauth_version="1.0"
)

我现在能做什么?

【问题讨论】:

    标签: php curl twitter twitter-oauth twitter-digits


    【解决方案1】:

    如果您有来自数字的 URL 和标题,那么您可以使用以下代码:

    $apiUrl = 'https://api.digits.com/1.1/sdk/account.json';
    $authHeader = 'OAuth oauth_consumer_key="**********", oauth_nonce="****", oauth_signature="****", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1481554529", oauth_token="*****", oauth_version="1.0"';
    
    // Create a stream
    $opts = array(
      'http'=>array(
        'method'=>"GET",
        'header'=>"Authorization: {$authHeader}"
      )
    );
    
    $context = stream_context_create($opts);
    
    // Open the file using the HTTP headers set above
    $file = file_get_contents($apiUrl, false, $context);
    
    $final_output = array();
    if($file)
    {
        $final_output = json_decode($file,true);
    }
    print_r($final_output);
    

    它将返回 id_str、电话号码等。您可以将这些数据存储在您的数据库中。

    【讨论】:

      猜你喜欢
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多