在PHP中执行$ _post时出错

时间:2017-01-15 15:34:42

标签: php post request error-log

好的,我正在使用我在这里找到的一些代码:Read feed on a Facebook page without login in

代码如下

proxy.php

<?php
// always add this header to ensure the JSON is output in the correct format
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header('Content-Type: application/json; charset=utf-8'); 

$graphUrl = $_POST[graphUrl];
if ($graphUrl == "") {
    $graphUrl = "https://graph.facebook.com/684985445000793/posts";
}

//App Info, needed for Auth
$app_id = "MY-APP-ID";
$app_secret = "MY-APP-SECRET";

//retrieve auth token
$authToken = fetchUrl("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={$app_id}&client_secret={$app_secret}");

//Echo back json to read client side.
echo fetchUrl("{$graphUrl}?fields=ratings%2Cposts.limit(10)&access_token={$authToken}");

function fetchUrl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    $retData = curl_exec($ch);
    curl_close($ch); 
    return $retData;
}
?>

的index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>FB reader</title>
</head>
<body>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {

            var timeout = 5000,
                load_error;

            load_error = function (jqXHR, textStatus, errorThrown) {
                if (errorThrown === "timeout") {
                    alert('Server bussy');
                } else {
                    alert('error: 404', textStatus + ": " + errorThrown);
                }
            };      

            $(document).ready(function() {
                console.log('Loading from Facebook...\n');

                $.ajax({
                    type: 'POST',
                    url: 'proxy.php',
                    data: {graphUrl: 'https://graph.facebook.com/684985445000793/'}, 
                    timeout:  timeout,
                    error: load_error,
                    success: function (rv) {
                        var data = rv.data,
                            len = data.length,
                            i,
                            out = '';
                        for (i = 0; i < len; i += 1) {
                            if (data[i].description) {
                                out += data[i].description + '\n\n';
                            }
                        }
                        console.log(out);
                    }
                });

            });
        });
    </script>
</body>
</html>

我的问题是那个

 type: 'POST',
                    url: 'proxy.php',
                    data: {graphUrl: 'https://graph.facebook.com/684985445000793/'}

似乎没有在我的proxy.php的错误日志中工作我被告知:

  

PHP注意:使用未定义的常量graphUrl - 假设'graphUrl'   在第8行的proxy.php中

所以我假设数据:来自我的index.html没有被发送。

我收到此错误的任何帮助或建议?

重要的是我在共享主人身上......

1 个答案:

答案 0 :(得分:2)

$graphUrl = $_POST[graphUrl];应为$graphUrl = $_POST['graphUrl'];