0

我希望我的 instagram 网站的第一页加载照片存储照片 ID 等,并且当用户单击“like”按钮时,likes 将被触发到 likes.php,这会将like 发送到 instagram。下面的例子:

<?php

require 'instagram.class.php';

$instagram = new Instagram(array(
  'apiKey'      => '******************',
  'apiSecret'   => '******************',
  'apiCallback' => 'http://****/****/index.php'
));

$token = // code that generates and properly stores token;
$instagram->setAccessToken($token);
$id =  $_GET['pic'];
$instagram->likeMedia($id);

if ($result->meta->code === 200) {
  echo 'Success! The image was added to your likes.';
} else {
  echo 'Something went wrong :(';
}


?>

例如,让我们说现在发送喜欢的页面看起来像:

 echo "<img src='like.jpg' class='like' onclick='SendID("4358734534_5435435")'>";

SendId 应该是什么样的?或者有什么好的方法来完成这项任务,我只是让您能够单击上面创建的按钮并将该照片的 id 发送到 likes.php

4

2 回答 2

1

onclick is a so-called "event handler". Usually, they are written in JavaScript.

Your example is a JavaScript function called SendID.

Your mentioned page (likes.php) would have to be visited using Ajax. One of the easiest ways to implement it, is using jQuery.

Read more about this in the references below.

References:

于 2013-08-14T16:49:49.450 回答
1

Sample:

echo "<img src='like.jpg' class='like' onclick='likePic(".$picId.",".$userId.")'>";

Using JQuery http://jquery.com/:

function likePic(picId, userId){

    $.get('path/to/like.php?picid=' + picId + '&userid=' + userId+ "&r=" + (new Date().getTime()), function(data) {
       if (data != "ok") {
         //maybe error handling, if like.php does not return "ok".
       }
    }
}

without jQuery (plain JavaScript)

function likePic(picId, userId){
    var xhr = new XMLHttpRequest();
    xhr.open("GET", 'path/to/like.php?picid=' + picId + '&userid=' + userId+ "&r=" + (new Date().getTime()), true);
    xhr.onload = function (e) {
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          if (xhr.responseText != "ok") {
             //maybe error handling, if like.php does not return "ok".
           }
        }
      }
    };
   xhr.send(null);
  }

the "&r=" + (new Date().getTime()) of both examples is just to avoid Caching of the result by the webbrowser.

于 2013-08-14T16:50:39.823 回答