执行POST从AJAX到PHP脚本Facebook的图形API响应数组 [英] POSTing an Array from a Facebook Graph API response by AJAX to a PHP script

查看:128
本文介绍了执行POST从AJAX到PHP脚本Facebook的图形API响应数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello_I'm使得Facebook的画布应用程序,我试图获得邀请好友的过程工作。我有我多的朋友选择做,它的正确发送邀请,但我现在想报答邀请者每当邀请的人增加了应用程序。

Hello_I’m making a Facebook Canvas App and I’m trying to get the Invite Friends process working. I have my multi-friend selector made, and it’s sending invites correctly, but I now want to reward the inviter whenever an invited person adds the app.

我想这样做的正确的方法是,每当一个新的用户开始使用的应用程序运行检查。我想通过查询Facebook的图形API来做到这一点,看看新的用户有一个 AP prequest 我的应用程序,如果他们这样做的话,我想找出已将这个新的用户邀请请求谁的人/人的ID(或标识)。

I think the correct way to do this is to run a check whenever a new user begins using the app. I’m trying to do this by querying Facebook’s Graph API to see if the new user has an apprequest for my app, if they do, then I want to find out the ID (or IDs) of the person/people who have sent this new user an invite request.

要努力工作,如何做到这一点,我有3个邀请(来自3个不同的帐户)发送到一个帐户,现在如果我登录的邀请接收器,然后查询Facebook的图形API控制台与此code: /v2.3/me/ap$p$pquests?fields=from,id 返回这样的:

To try to work out how to do this, I had 3 invites (from 3 different accounts) sent to one account, and now if I log in as the invite receiver and then query Facebook’s Graph API console with this code: /v2.3/me/apprequests?fields=from,id it returns this:

{"data": [
    {
      "from": {
        "id": "1234526161534",
        "name": "John Doe"
      },
      "id": "14677152342565_827362417384",
      "created_time": "2015-05-19T21:35:11+0000"
    },
    {
      "from": {
        "id": "4777221325314",
        "name": "Jane Doe"
      },
      "id": "1683273626362_827362417384",
      "created_time": "2015-05-19T20:50:14+0000"
    },
    {
      "from": {
        "id": "1015521200905293",
        "name": "Di Do"
      },
      "id": "1251333932735_827362417384",
      "created_time": "2015-05-19T20:50:08+0000"
    }
  ]}

所以它显示我,3个不同的人给我发了 AP prequests 的应用程序。它给我自己的ID,他们的名字,以及独特的请求ID串联起来的邀请人的身份证。

So it’s showing me that 3 different people have sent me apprequests for the app. It’s giving me their IDs, their names, and a unique request ID concatenated with the Invited person’s ID.

目前,我能够单独抢发送者的ID,但我想送他们作为数组由AJAX PHP脚本,然后我想用一个PDO prepared声明将它们插入到我的数据库如果可能的话。

Currently, I'm able to grab the sender IDs individually, but I want to send them as an array to a PHP script by AJAX, and then I want to insert them into my database using a PDO prepared statement if that's possible.

这是我目前的code:

This is my current code:

/* making the API call */
FB.api(
    'me/apprequests?fields=from,id',
    function (response) {
     if (response && !response.error) {

   var requestid = response.data[0].from.id;
   var requestid1 = response.data[1].from.id;
   var requestid2 = response.data[2].from.id;

   console.log(response.data[0].from.id);
   console.log(response.data[1].from.id);
   console.log(response.data[2].from.id); 

    $.ajax({
         url: 'reward.php',
         data: {'requestid' : requestid,
               'requestid1' : requestid1,
               'requestid2' : requestid2},
        type: "POST",
        success: function(response){
          if(response==1){
             alert("Reward Applied!");
                 }
          else{
             alert("Reward Failed!");
                 }
            }
       });
      }
    }
);

虽然我能够的ID发送给PHP文件作为独立变量 VAR请求ID,VAR requestid1,VAR requestid2 键,然后将他们的个人,这不是一个好的方法来使用,作为请求ID的数量将有所不同,所以不是送他们每个人作为独立变量,我想我需要向他们发送一个数组,但我不确定如何做到这一点。

While I am able to send the IDs to the PHP file as separate variables var requestid, var requestid1, var requestid2 and then Insert them individually, this isn't a good method to use, as the number of request IDs will vary, so instead of sending each of them as individual variables, I think I need to send them as an array, but I'm unsure of how to do this.

我目前的PDO将交易是这样的:

My current PDO Insert Transaction is this:

reward.php

<?php
$servername = "myservername";
$username = "myusername";
$password = "mypassword";
$dbname = "mydbname";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $conn->beginTransaction();

    // Defining the three request IDs
    $requestid = $_POST['requestid'];
    $requestid1 = $_POST['requestid1'];
    $requestid2 = $_POST['requestid2'];

    // Insert first request ID
    $stmt = $conn->prepare("INSERT INTO requestids (requestid) VALUES (:requestid)");
    $stmt->bindParam(':requestid', $requestid);
    $requestid = $_POST['requestid'];
    $stmt->execute();

    // Insert second request ID
    $stmt = $conn->prepare("INSERT INTO requestids (requestid) VALUES (:requestid1)");
    $stmt->bindParam(':requestid1', $requestid1);
    $requestid = $_POST['requestid1'];
    $stmt->execute();

    // Insert third request ID
    $stmt = $conn->prepare("INSERT INTO requestids (requestid) VALUES (:requestid2)");
    $stmt->bindParam(':requestid2', $requestid2);
    $requestid = $_POST['requestid2'];
    $stmt->execute();

    $conn->commit();
    echo "1";
    }
catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }
$conn = null;
?>

这是行不通的,如果一个人有来自超过3人的要求,它真正需要的是一个数组代替。我是pretty的新PDO和交易,并没有任何使用数组的工作经验。 我真的AP preciate任何帮助都与此,在此先感谢这么多!

This won't work if a person has requests from more than 3 people, it really needs to be an Array instead. I'm pretty new to PDO and Transactions, and don't have any experience working with Arrays. I'd really appreciate any help at all with this, thanks so much in advance!

推荐答案

@Emily关于更新的问题,它很容易实现。通过数组本身阿贾克斯后像这样

@Emily With respect to your updated questions, its easy to achieve. Pass the array itself to ajax post like this

$.ajax({
     url: 'reward.php',
     data: {'requestids' : response.data},
    type: "POST",
    success: function(response){
      if(response==1){
         alert("Reward Applied!");
             }
      else{
         alert("Reward Failed!");
             }
        }
   });
  }
}

然后在你的PHP遍历数组requestids并生成MySQL查询所有记录插入一次像这样

Then in your PHP loop through requestids array and generate mysql query to insert all records at once like this

$sql= "INSERT INTO requestids (requestid) VALUES "; 
foreach($_POST['requestids'] as $requestId){
       $sql.="($requestId)";
}

我相信你可以很容易地转换这种简单的查询到PDO声明

I am sure you can easily convert this simple query into a PDO statement

这篇关于执行POST从AJAX到PHP脚本Facebook的图形API响应数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆