douqiao7188 2013-08-14 16:40
浏览 30
已采纳

简单的PHP请求。 将请求从第一页发送到第二页而不登陆页面

I would like for page one of my instagram site to load the photos store the photo ids etc, and when a user clicks on the like button, the like will be fired off to likes.php which will send the like to instagram. example below:

<?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 :(';
}


?>

and for examples sake lets say that the page sending the like for right now looks like:

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

What should SendId look like? or what would be good to look into to get this task done, I just you to be able to click the button created above and send the id of that photo to likes.php

  • 写回答

2条回答 默认 最新

  • duanjiaoxi4928 2013-08-14 16:50
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)