简体   繁体   English

如何解析从图表Api返回的数据

[英]How do I parse data returned from the Graph Api

I have a PHP form (index.php) that uses the Facebook Graph API to create an event. 我有一个PHP表单(index.php),它使用Facebook Graph API来创建一个事件。 A form is used to capture user input, and this data is POSTed back to the API to create the event. 表单用于捕获用户输入,并将此数据POST回API以创建事件。 This is the same code as explained in this tutorial: http://www.masteringapi.com/tutorials/how-to-create-facebook-events-using-graph-api/49/ 这与本教程中解释的代码相同: http//www.masteringapi.com/tutorials/how-to-create-facebook-events-using-graph-api/49/

The result of this is that, it returns the ID of the event that just got created. 结果是,它返回刚刚创建的事件的ID。

    <?php

    $app_id = "APP_key";
    $app_secret = "APP_secret";
    $my_url = "URL";

    $code = $_REQUEST["code"];

    if(empty($code)) {
    $auth_url = "http://www.facebook.com/dialog/oauth?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url)
    . "&scope=create_event";
    echo("<script>top.location.href='" . $auth_url . "'</script>");
    }

    $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url)
    . "&client_secret=" . $app_secret
    . "&code=" . $code;
    $access_token = file_get_contents($token_url);

    $event_url = "https://graph.facebook.com/me/events?" . $access_token;

    ?>

    <style>
    label {float: left; width: 100px;}
    input[type=text],textarea {width: 210px;}
    </style>
    </head>
    <body>

    <div id="inputForm"> 
    <form enctype="multipart/form-data" action="<?php echo $event_url; ?>" method="post">
    <p><label for="name">Event Name</label><input type="text" name="name" value="" /></p>
    <p><label for="description">Event Description</label><textarea name="description"></textarea></p>
    <p><label for="location">Location</label><input type="text" name="location" value="" /></p>
    <p><label for="">Start Time</label><input type="text" name="start_time" value="<?php echo date('Y-m-d H:i:s'); ?>" /></p>
    <p><label for="end_time">End Time</label><input type="text" name="end_time" value="<?php echo date('Y-m-d H:i:s', mktime(0, 0, 0, date("m")  , date("d")+1, date("Y"))); ?>" /></p>
    <p><label for="picture">Event Picture</label><input type="file" name="picture" /></p>
    <p>
        <label for="privacy_type">Privacy</label>
        <input type="radio" name="privacy_type" value="OPEN"   checked='checked'/>Open&nbsp;&nbsp;&nbsp;
        <input type="radio" name="privacy_type" value="CLOSED" />Closed&nbsp;&nbsp;&nbsp;
        <input type="radio" name="privacy_type" value="SECRET" />Secret&nbsp;&nbsp;&nbsp;
   </p>
   <p><input type="submit" value="Create Event" /></p>
   </form>
   </div>

   <?php 

This code works great, but I'm trying to modify this code such that, index.php posts data to $event_url to create the event, but redirects you to a confirmation page that displays a message such as: 这段代码效果很好,但我正在尝试修改此代码,以便index.php将数据发布到$ event_url以创建事件,但会将您重定向到显示消息的确认页面,例如:

"Event successfully created! Click here to access your event", and clicking "here" would redirect you the event on Facebook. “活动已成功创建!点击此处访问您的活动”,点击“此处”会将您的活动重定向到Facebook。

Any thoughts on how this could be done? 有关如何做到这一点的任何想法?

Change $event_url to a page like event_submitter.php on your site and add the access_token as a hidden field on your form. $event_url更改$event_url您网站上的event_submitter.php的网页,并将access_token添加为表单上的隐藏字段。

The event_submitter page should take the $_POST data, strip out the access_token (You could eliminate this part if you used the php SDK), reformat the rest of the $_POST data, send this to to the Facebook API via cURL and examine the response. event_submitter页面应该获取$_POST数据,去掉access_token (如果你使用了php SDK,你可以删除这部分),重新格式化其余的$_POST数据,通过cURL将其发送到Facebook API并检查响应。

If you get the correct response, you can then generate the output you want from that. 如果得到正确的响应,则可以从中生成所需的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM