This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
forked from facebookarchive/web-scrumptious
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-to-facebook.php
81 lines (71 loc) · 2.42 KB
/
post-to-facebook.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
header( 'Content-Type: application/json; charset=utf8', true );
if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || $_SERVER['REQUEST_METHOD'] !== 'POST' ) {
if ( ! headers_sent() ) {
header( 'HTTP/1.1 405 Method Not Allowed', true, 405 );
header( 'Allow: POST' );
}
echo json_encode( array( 'error' => 'POST only' ) );
exit();
}
require_once( dirname(__FILE__) . '/meals/meal-options.php' );
if ( ! isset( $_POST['meal'] ) || ! isset( $meal_options[ $_POST['meal'] ] ) ) {
if ( ! headers_sent() )
header( 'HTTP/1.1 400 Bad Request', true, 400 );
echo json_encode( array( 'error' => 'no valid meal specified or no app namespace set' ) );
}
// load app configuration data
require_once( dirname(__FILE__) . '/config.php' );
$params = array(
'meal' => Facebook_Sample_Application::BASE_URI . 'meals/meal.php?' . http_build_query( array( 'id' => $_POST['meal'] ) ),
'fb:explicitly_shared' => true
);
// custom message
if ( isset( $_POST['message'] ) && $_POST['message'] ) {
$message = trim( $_POST['message'] );
if ( $message )
$params['message'] = $message;
unset( $message );
}
// action tags
if ( isset( $_POST['tags'] ) && $_POST['tags'] ) {
$tags = explode( ',', trim( $_POST['tags'] ) );
$valid_tags = array();
foreach( $tags as $tag ) {
if ( ctype_digit( $tag ) )
$valid_tags[] = $tag;
}
unset( $tags );
if ( ! empty( $valid_tags ) )
$params['tags'] = implode( ',', $valid_tags );
unset( $valid_tags );
}
// place
if ( isset( $_POST['place'] ) && ctype_digit( trim( $_POST['place'] ) ) )
$params['place'] = trim( $_POST['place'] );
// load Facebook PHP SDK
require_once( dirname(__FILE__) . '/vendor/facebook/php-sdk/src/facebook.php' );
$facebook = new Facebook( array(
'appId' => Facebook_Sample_Application::APP_ID,
'secret' => Facebook_Sample_Application::APP_SECRET
) );
$facebook->setExtendedAccessToken();
if ( $facebook->getUser() && Facebook_Sample_Application::APP_NS ) {
try {
$result = $facebook->api( 'me/' . Facebook_Sample_Application::APP_NS . ':eat', 'POST', $params );
} catch( FacebookApiException $e ) {
header( 'HTTP/1.1 500 Internal Server Error', true, 500 );
echo json_encode( array( 'error' => $e->getCode() . ' ' . $e->getMessage() ) );
exit();
}
if ( isset( $result ) )
echo json_encode( $result );
else
echo '{}';
} else {
if ( ! headers_sent() )
header( 'HTTP/1.1 403 Forbidden', true, 403 );
echo json_encode( array( 'error' => 'must be logged in to Facebook' ) );
exit();
}
?>