2017-09-13 102 views
1

我正在編寫一個PHP腳本,通過GitHub API(https://api.github.com/users/{username}/events)找到用戶的GitHub電子郵件地址。Phraseing PHP中的GitHub JSON API

我想過一種方法來做到這一點,但我很難實現它到PHP中。我的思考過程是拉JSON,搜索'電子郵件'字符串(使用循環和一些正則表達式),然後返回結果。

這是我的PHP至今(我還是新的語言): https://hastebin.com/wuzezotuqi.xml

+0

你需要將電子郵件地址存儲到服務器端或其唯一目的是顯示在客戶端? –

+0

@RatulSharker它只需要顯示在客戶端 – Undesirable

+0

@RatulSharker對不起,遲到的迴應,你能告訴我服務器端(PHP)提出的解決方案嗎?我真的很感激它。 – Undesirable

回答

0

您可以使用JavaScript的客戶端瀏覽器獲取的電子郵件。

//parse the response for the email. 
function reqListener() { 
    var para = document.getElementById('textpad'); 
    var emailIndex = this.responseText.indexOf("email"); 

    var nextColonIndex = this.responseText.indexOf(":", emailIndex); 
    var nextCommanIndex = this.responseText.indexOf(",", emailIndex); 

    var emailAddr = this.responseText.substr(nextColonIndex + 1, nextCommanIndex-nextColonIndex - 1); 
} 


// 
// making the api request 
// 
var oReq = new XMLHttpRequest(); 
oReq.addEventListener("load", reqListener); 
oReq.open("GET", "https://api.github.com/users/ratulSharker/events"); 
oReq.send(); 

檢查它的行動在jsfiddle

櫃面你打算使用PHP做,在服務器端

<?php 

$config['useragent'] = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0'; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']); 
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/users/ratulSharker/events'); 
     $content = curl_exec($ch); 



$emailIndex = strpos($content, "email", 0);  // find from the start 


$nextColonIndex = strpos($content, ":", $emailIndex); 
$nextCommanIndex = strpos($content, "," , $emailIndex); 
$nextQuoteIndex = strpos($content, "\"", $nextColonIndex); 
$nextNextQuoteIndex = strpos($content, "\"", $nextQuoteIndex+1); 



    $emailAddr = substr($content, $nextQuoteIndex, $nextNextQuoteIndex-$nextQuoteIndex+1); 

echo $emailAddr; 

檢查行動中的代碼PHPFiddle