Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用原始PHP的linkedin API_Php_Linkedin - Fatal编程技术网

使用原始PHP的linkedin API

使用原始PHP的linkedin API,php,linkedin,Php,Linkedin,我想在获得令牌后获得配置文件信息 <?php session_start (); if (!$_SESSION['linkedin_access_token']) { echo 'Grant access first'; exit (); } ## step 0 define ('LINKEDIN_KEY', 'xxxxxx'); define ('LINKEDIN_SECRET', 'xxxxx

我想在获得令牌后获得配置文件信息

<?php

    session_start ();

    if (!$_SESSION['linkedin_access_token'])
    {
        echo 'Grant access first';
        exit ();
    }

    ## step 0
    define ('LINKEDIN_KEY', 'xxxxxx');
    define ('LINKEDIN_SECRET', 'xxxxxxx');

    function urlencode_oauth ($str)
    {
        return str_replace ('+', ' ', str_replace ('%7E', '~', rawurlencode ($str)));
    }

    $links = array (
        'request_token' => 'https://api.linkedin.com/uas/oauth/requestToken',
        'authorize' => 'https://www.linkedin.com/uas/oauth/authorize',
        'access_token' => 'https://api.linkedin.com/uas/oauth/accessToken',
    );


    ## step 2
    $params = array (
        'oauth_consumer_key' => LINKEDIN_KEY,
        'oauth_nonce' => sha1 (microtime ()),
        'oauth_signature_method' => 'HMAC-SHA1',
        'oauth_timestamp' => time (),
        'oauth_token' => $_SESSION ['linkedin_access_token'],
        'oauth_version' => '1.0'
    );

    ## step 3
    // sort parameters according to ascending order of key
    // sort parameters according to ascending order of key
    ksort ($params);

    // prepare URL-encoded query string
    $q = array ();
    foreach ($params as $key => $value)
    {
        $q [] = urlencode_oauth ($key) . '=' . urlencode_oauth ($value);
    }
    $q = implode ('&', $q);

    // generate the base string for signature
    $parts = array (
        'POST',
        urlencode_oauth ('https://api.linkedin.com/v1/people/~'),
        urlencode_oauth ($q)
    );
    $base_string = implode ('&', $parts);

    ## step 4
    $key = urlencode_oauth (LINKEDIN_SECRET) . '&' . urlencode_oauth ($_SESSION ['linkedin_access_token_secret']);
    $signature = base64_encode (hash_hmac ('sha1', $base_string, $key, true));

    ## step 5
    $params ['oauth_signature'] = $signature;
    $str = array ();
    foreach ($params as $key => $value)
    {
        $str [] = $key . '="' . urlencode_oauth ($value) . '"';
    }
    $str = implode(', ', $str);
    $headers = array (
        'POST /v1/people/~ HTTP/1.1',
        'Host: api.linkedin.com',
        'Authorization: OAuth ' . $str,
        'Content-Type: text/xml;charset=UTF-8',
        'Content-Length: 0',
        'Connection: close'
    );

    ## step 6
    $fp = fsockopen ("ssl://api.linkedin.com", 443, $errno, $errstr, 30);
    if (!$fp)
    {
        echo 'Unable to connect to LinkedIn';
        exit();
    }
    $out = implode ("\r\n", $headers) . "\r\n\r\n";
    fputs ($fp, $out);

    // getting LinkedIn server response
    $res = '';
    while (!feof ($fp)) $res .= fgets ($fp, 4096);
    fclose ($fp);

    echo '<pre>';
    echo $res . "\n\n";
    echo $_SESSION ['linkedin_access_token'] . "\n" . $_SESSION ['linkedin_access_token_secret'];

 ?>

怎么了?它告诉我

define('LINKEDIN_KEY', 'YOUR_KEY');
define('LINKEDIN_SECRET', 'YOUR SECRET');

function urlencode_oauth($str) {
return str_replace('+',' ',str_replace('%7E','~',rawurlencode($str)));
}
$links = array(
 'request_token'=>'https://api.linkedin.com/uas/oauth/requestToken',
  'authorize'=>'https://www.linkedin.com/uas/oauth/authorize',
  'access_token'=>'https://api.linkedin.com/uas/oauth/accessToken'
);
$params = array(
  'oauth_callback'=>"YOUR CALLBACK URL",
  'oauth_consumer_key'=>LINKEDIN_KEY,
  'oauth_nonce'=>sha1(microtime()),
  'oauth_signature_method'=>'HMAC-SHA1',
  'oauth_timestamp'=>time(),
  'oauth_version'=>'1.0'
);
    // sort parameters according to ascending order of key
    ksort($params);

    // prepare URL-encoded query string
    $q = array();
    foreach ($params as $key=>$value) {
      $q[] = urlencode_oauth($key).'='.urlencode_oauth($value);
    }
    $q = implode('&',$q);

    // generate the base string for signature
    $parts = array(
      'POST',
      urlencode_oauth($links['request_token']),
      urlencode_oauth($q)
    );
    $base_string = implode('&',$parts);
    $key = urlencode_oauth(LINKEDIN_SECRET) . '&';
    $signature = base64_encode(hash_hmac('sha1',$base_string,$key,true));

    $params['oauth_signature'] = $signature;
    $str = array();
    foreach ($params as $key=>$value) {
      $str[] = $key . '="'.urlencode_oauth($value).'"';
    }
    $str = implode(', ',$str);
    $headers = array(
      'POST /uas/oauth/requestToken HTTP/1.1',
      'Host: api.linkedin.com',
      'Authorization: OAuth '.$str,
      'Content-Type: text/xml;charset=UTF-8',
      'Content-Length: 0',
      'Connection: close'
    );
    $fp = fsockopen("ssl://api.linkedin.com",443,$errno,$errstr,30);
    if (!$fp) { echo 'Unable to connect to LinkedIn'; exit(); }
    $out = implode("\r\n",$headers) . "\r\n\r\n";
    fputs($fp,$out);

    // getting LinkedIn server response
    $res = '';
    while (!feof($fp)) $res .= fgets($fp,4096);
    fclose($fp);

    $parts = explode("\n\n",str_replace("\r",'',$res));
    $res_headers = explode("\n",$parts[0]);
    if ($res_headers[0] != 'HTTP/1.1 200 OK') {
      echo 'Error getting OAuth token and secret.'; exit();
    }
    parse_str($parts[1],$data);
    if (empty($data['oauth_token'])) {
      echo 'Failed to get LinkedIn request token.'; exit();
    }
HTTP/1.1405方法不允许
服务器:ApacheCoote/1.1
x-li-request-id:H8M76QXW5O
日期:2012年9月4日星期二12:09:21 GMT
变化:*
x-li-format:xml
内容类型:text/xml;字符集=UTF-8
内容长度:262
405
1346760561727
H8M76QXW5O
0
不支持的发布目标{/v1/people/~}
XXXXXXXXXXXXXX
XXXXXXXXXXXXXX
作为,配置文件API不支持POST方法。尝试使用GET来检索配置文件数据

替换三件事1<代码>您的_键'2'<代码>你的秘密和3.
oauth\u回调

谢谢


Anand

正在编写的替代帖子显示:HTTP/1.1 401未经授权的服务器:Apache Coyote/1.1 x-li-request-id:KN3IEFYXD日期:周二,2012年9月4日16:24:19 GMT变化:*x-li-format:xml内容类型:text/xml;字符集=UTF-8内容长度:341 401 1346775860118 KN3IEFYXD 0[未经授权]。OAU:jyedyo3jrxcq | 64d50e16-2e22-4d76-a01b-cca5e5fdf073 |*01 |*01:1346775859:yugqbx10rogb8loldcsrrlrfenpg=GET是正确的方法-您得到的错误似乎表明OAuth本身有问题-尝试确保所有东西都与您的键一起工作,并且它是您的endWow上的OAuth问题,工作正常。我只在一个地方换了一个职位,在两个地方换了一个职位就像一个符咒。再次感谢你,当我尝试时,代码不起作用,请你有什么想法吗?请按照这个,用你自己的“LINKEDIN\u密钥”、“LINKEDIN\u秘密”和“oauth\u回调”替换三件东西
define('LINKEDIN_KEY', 'YOUR_KEY');
define('LINKEDIN_SECRET', 'YOUR SECRET');

function urlencode_oauth($str) {
return str_replace('+',' ',str_replace('%7E','~',rawurlencode($str)));
}
$links = array(
 'request_token'=>'https://api.linkedin.com/uas/oauth/requestToken',
  'authorize'=>'https://www.linkedin.com/uas/oauth/authorize',
  'access_token'=>'https://api.linkedin.com/uas/oauth/accessToken'
);
$params = array(
  'oauth_callback'=>"YOUR CALLBACK URL",
  'oauth_consumer_key'=>LINKEDIN_KEY,
  'oauth_nonce'=>sha1(microtime()),
  'oauth_signature_method'=>'HMAC-SHA1',
  'oauth_timestamp'=>time(),
  'oauth_version'=>'1.0'
);
    // sort parameters according to ascending order of key
    ksort($params);

    // prepare URL-encoded query string
    $q = array();
    foreach ($params as $key=>$value) {
      $q[] = urlencode_oauth($key).'='.urlencode_oauth($value);
    }
    $q = implode('&',$q);

    // generate the base string for signature
    $parts = array(
      'POST',
      urlencode_oauth($links['request_token']),
      urlencode_oauth($q)
    );
    $base_string = implode('&',$parts);
    $key = urlencode_oauth(LINKEDIN_SECRET) . '&';
    $signature = base64_encode(hash_hmac('sha1',$base_string,$key,true));

    $params['oauth_signature'] = $signature;
    $str = array();
    foreach ($params as $key=>$value) {
      $str[] = $key . '="'.urlencode_oauth($value).'"';
    }
    $str = implode(', ',$str);
    $headers = array(
      'POST /uas/oauth/requestToken HTTP/1.1',
      'Host: api.linkedin.com',
      'Authorization: OAuth '.$str,
      'Content-Type: text/xml;charset=UTF-8',
      'Content-Length: 0',
      'Connection: close'
    );
    $fp = fsockopen("ssl://api.linkedin.com",443,$errno,$errstr,30);
    if (!$fp) { echo 'Unable to connect to LinkedIn'; exit(); }
    $out = implode("\r\n",$headers) . "\r\n\r\n";
    fputs($fp,$out);

    // getting LinkedIn server response
    $res = '';
    while (!feof($fp)) $res .= fgets($fp,4096);
    fclose($fp);

    $parts = explode("\n\n",str_replace("\r",'',$res));
    $res_headers = explode("\n",$parts[0]);
    if ($res_headers[0] != 'HTTP/1.1 200 OK') {
      echo 'Error getting OAuth token and secret.'; exit();
    }
    parse_str($parts[1],$data);
    if (empty($data['oauth_token'])) {
      echo 'Failed to get LinkedIn request token.'; exit();
    }