2016-11-17 66 views
0

我正在尝试使用Instagram PHP API来获取朋友照片。我注册为沙盒用户以及发布了几张随机图片的虚拟用户。然后,我邀请虚拟用户授予沙箱中的应用以连接其帐户。无法使用提供的PHP API从Instagram获取图片

为了获得访问令牌我发送一个请求得到代码,然后访问令牌与范围:

https://api.instagram.com/oauth/access_token?client_id=CLIENT_ID&client_secret=SECRET_STRING&grant_type=authorization_code&redirect_uri=http://localhost/&code=CODE&scope=basic+public_content+comments+likes` 

PHP代码:

$url = 'https://api.instagram.com/oauth/access_token'; 
    $payload = [ 
      $GLOBALS['KEY_CID'] => $GLOBALS['VAL_CID'], 
      $GLOBALS['KEY_CECRET'] => $GLOBALS['VAL_CECRET'], 
      'grant_type' => 'authorization_code', 
      'redirect_uri' => 'http://localhost/', 
      'code' => $param_code, 
      'scope' => 'basic+public_content+comments+likes']; 

$ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); // url 
     curl_setopt($ch, CURLOPT_POST, true); // POST 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); // POST DATA 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // RETURN RESULT true 
     curl_setopt($ch, CURLOPT_HEADER, 0); // RETURN HEADER false 
     curl_setopt($ch, CURLOPT_NOBODY, 0); // NO RETURN BODY false/we need the body to return 
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // VERIFY SSL HOST false 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // VERIFY SSL PEER false 
$response = json_decode(curl_exec($ch), true); 
curl_close($ch); 

我访问令牌后成功。

我尝试这是我的测试中创建一个虚拟帐户沙盒用户获得的图像:

https://api.instagram.com/v1/users/fane.leee/media/recent?access_token=ACCESS_TOKEN 

PHP代码检索图片:

$url = 'https://api.instagram.com/v1/users/fane.leee/media/recent?access_token='.$ACCESS_TOKEN; 
    $ch = curl_init(); // initializing 
    curl_setopt($ch, CURLOPT_URL, $url); // API URL to connect 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return the result, do not print 
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
    $response = json_decode(curl_exec($ch), true); // connect and get json data 
    curl_close($ch); 

问题
的获取图片请求的$响应是NULL,没有错误信息。任何人有关于这个问题的任何想法?

谢谢。


参考

  1. Instagram的开发文档 authenticationauthorization

  2. Instagram的开发文档约 user endpoints

  3. 另一个StackOverflow post约Instagram的API


开发环境: 的Ubuntu 16.04
的Apache2
PHP 7.0.8
MySQL的14.14


编辑1:添加调用cUrl函数的PHP代码。
编辑2:如果我使用'self'来替换用户标识'fane.leee',我可以检索我发布的图像。我也跟着用户'fane.leee'。还有什么我应该做的,以检索朋友的媒体?

+0

张贴一些代码,请。 –

+0

感谢您的意见,代码添加:) – r0ng

+1

您是否添加了任何沙箱用户? – Viney

回答

0

fane.leee是一个用户名。 Instagram需要在API调用者中使用用户标识。

要通过调用Instagram的API函数获得一个用户ID,我们可以使用
https://www.instagram.com/{username}/?__a=1

另一种方式来获得用户ID是使用第三方网站。它可以从用户名中检索Instagram用户标识。这是here


参考:
从Thinh的武的回答中this thread - Not the accepted answer, but the answer underneath. P.S the accepted answer is not working anymore.

相关问题