感觉这可能是一个愚蠢的问题,但经过一段时间寻找可能性并玩弄价值观之后,我似乎仍无法弄清楚如何做到这一点。
我基本上想要做的是从Rockstar Games Social Club获取帐户信息。我发现这个网站使用私有API。但为了使其工作,它需要与服务器进行活动会话。
如果没有身份验证,API将返回:
{"Portfolio":[{"CharacterName":"Michael","TotalShares":0,"TotalInvested":"$0","DayRange":"N/A","ChangePercent":"N/A","ChangeDollar":"N/A","ChangeDirection":"","Investments":[]},{"CharacterName":"Franklin","TotalShares":0,"TotalInvested":"$0","DayRange":"N/A","ChangePercent":"N/A","ChangeDollar":"N/A","ChangeDirection":"","Investments":[]},{"CharacterName":"Trevor","TotalShares":0,"TotalInvested":"$0","DayRange":"N/A","ChangePercent":"N/A","ChangeDollar":"N/A","ChangeDirection":"","Investments":[]}]}
所以,我试图找到一种方法来完成这项工作。我觉得cURL似乎是最好的方法。
但是,我测试的代码是将我重定向到他们的网站,而不是获取数据,之后我可以调用新请求,希望它可以正常工作。
我的PHP代码:
class DownloadJSON {
public function download($url) {
return file_get_contents($url);
}
public function login($url, $username, $password) {
$url = "https://socialclub.rockstargames.com/profile/signin";
$vars = "login-field=". $username ."&password-field=". $password . "&rememberme-field=off";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
$data = curl_exec($ch);
$me = $this->download($url);
curl_close($ch);
echo $me;
}
}
if(isset($_GET['url'])) {
if (strlen($_GET['url']) > 0) {
if ($_GET['action'] == "stocks") {
$downloadJSON = new DownloadJSON();
echo $downloadJSON->download($_GET['url']);
}
else if ($_GET['action'] == "portfolio"){
$downloadJSON = new DownloadJSON();
echo $downloadJSON->login($_GET['url'], $_GET['username'], $_GET['password']);
}
else {
echo json_encode("invalid request");
}
}
}
您诚挚的, Larssy1
答案 0 :(得分:0)
您可以尝试设置CURLOPT_COOKIEFILE
,以便实际保存Cookie会话。但是,在下一个请求中,您只是执行file_get_contents
,因此可能会忽略设置的Cookie。
尝试使用CURL从API中检索内容。
此外,API可能是私有的。