<?php  
$api_key = 'xxx'; 
$secret = 'xxx'; 
$url = 'http://rest.kaixin001.com/api/rest.php'; 
$session_key = $_GET['session_key']; 
if (empty($session_key)) { 
     $session_key =   $_COOKIE["kx_connect_session_key"]; 
} else { 
     setcookie("kx_connect_session_key", $session_key, time()+3600*4); 

$param = array( 
'api_key' => $api_key, 
'method' => 'users.getInfo', 
'uids' => '100099,100100', 
'format' => 'json', 
'session_key' => $session_key, 
); 
$query = buildQuery($param, $secret); 
$result = postRequest($url, $query); 
$result = json_decode($result); 
function buildQuery($param, $secret) { 
     $param['call_id'] = microtime(true); 
     $param['v'] = '1.0'; 
     ksort($param);   
     $request_str = ''; 
     foreach ($param as $key => $value) { 
         $request_str .= $key . '=' . $value; //  没有分割符 
     } 
     $sig = $request_str . $secret; 
     $sig = md5($sig); 
     $param['sig'] = $sig; 
     $query = http_build_query($param); 
     return $query; 

function postRequest($url, $post_string) {  
     $useragent = 'kaixin001.com API PHP5 Client 1.1 (curl) ' . phpversion(); 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     if (strlen($post_string) >= 3) { 
         curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); 
     } 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
     curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
     $result = curl_exec($ch); 
     curl_close($ch); 
     return $result; 

function url_base64_encode($str) 

$search = array ('+', '/'); 
$replace = array ('*', '-'); 
$basestr = base64_encode($str); 
return str_replace( $search, $replace, $basestr ); 
}大家好,这个是我在链接开心网的openID时调用getInfo时的代码,请大家解释一下,最好是每行都解释一下。分不够可以再加

解决方案 »

  1.   

    构造出$param数组,利用http_build_query将数组生成一个URL请求字符串,   ===> 自定义buildQuery函数
    利用php的CURL模拟提交数据,没有设定CURLOPT_POST
    将前面的字符串以GET方式提交到$url = 'http://rest.kaixin001.com/api/rest.php'; ===> 自定义postRequest函数$result = json_decode($result);返回结果数据从json转换为数组格式url_base64_encode函数这段代码没用上
      

  2.   

    构造出$param数组,利用http_build_query将数组生成一个URL请求字符串, ===> 自定义buildQuery函数
    利用php的CURL模拟提交数据,没有设定CURLOPT_POST
    将前面的字符串以GET方式提交到$url = 'http://rest.kaixin001.com/api/rest.php'; ===> 自定义postRequest函数$result = json_decode($result);返回结果数据从json转换为数组格式url_base64_encode函数这段代码没用上
      

  3.   

    楼上的厉害啊。直接照超啊,佩服,我对PHP不了解,帮你顶了!
      

  4.   

    2楼虽然解释的很好,但是我还是有些不懂,我没有学过php,可以解释再细一点吗