textfbapi(代码片段)

author author     2022-12-01     236

关键词:

<?php 
require_once get_template_directory().'/Facebook/autoload.php';
if ( !function_exists('media_handle_upload') ) 
    require_once(ABSPATH . "wp-admin" . '/includes/image.php');
    require_once(ABSPATH . "wp-admin" . '/includes/file.php');
    require_once(ABSPATH . "wp-admin" . '/includes/media.php');

global $wpdb;
$current_url = home_url(add_query_arg(array(),$_SERVER['REQUEST_URI']));
if (isset($_GET['act']) && $_GET['act']=='fbapp_edit' && isset($_GET['id'])) 
    $errors = '';
    $id = $_GET['id'];
    $sql_prep = $wpdb->prepare("SELECT * FROM ".$wpdb->prefix."fbapp WHERE ID = %s", $id);
    $record = $wpdb->get_row($sql_prep, OBJECT);
    $fbapp_id = $record->fbapp_id;
    $fbapp_name = $record->fbapp_name;
    $fbapp_secret = $record->fbapp_secret;
    $fbapp_userid = $record->fbapp_userid;
    $fbapp_token = $record->fbapp_token;    
    $fbapp_pages = $record->fbapp_pages;
    $fbapp_page_crawler = $record->fbapp_page_crawler;
    $fbapp_get_type = $record->fbapp_get_type;

if (isset($_POST['save_record'])) 
	$fbapp_id = $_POST['fbapp_id'];
    $fbapp_name = $_POST['fbapp_name'];
	$fbapp_secret = $_POST['fbapp_secret'];
	$fbapp_userid = $_POST['fbapp_userid'];
    $fbapp_token = $_POST['fbapp_token'];
    $fbapp_pages = $_POST['fbapp_pages'];
    $fbapp_page_crawler = $_POST['fbapp_page_crawler'];
    $fbapp_get_type = $_POST['fbapp_get_type'];
	$fields = array();
	$fields['fbapp_id'] = $fbapp_id;
    $fields['fbapp_name'] = $fbapp_name;
	$fields['fbapp_secret'] = $fbapp_secret;
	$fields['fbapp_userid'] = $fbapp_userid;
    $fields['fbapp_token'] = $fbapp_token;
    $fields['fbapp_pages'] = $fbapp_pages;
    $fields['fbapp_page_crawler'] = $fbapp_page_crawler;
    $fields['fbapp_get_type'] = $fbapp_get_type;
	$where = array('ID'=>$_GET['id']);
	$updated = $wpdb->update($wpdb->prefix.'fbapp', $fields, $where);
	if($updated)
		wp_redirect(get_page_link('settings'));die;
	

if(isset($_POST['fb_auth']))
    $my_url=urlencode('http://it.thietkewebwp.info/settings/?act=fbapp_edit&id=1');
    $fbapp_session_state = md5(uniqid(rand(), TRUE));
    setcookie("fbapp_session_state",$fbapp_session_state,"0","/");
    $dialog_url = "https://www.facebook.com/".FBAP_FB_API_VERSION."/dialog/oauth?client_id="
                . $fbapp_id . "&redirect_uri=" . $my_url . "&state="
                . $fbapp_session_state . "&scope=email,public_profile,publish_pages,user_posts,publish_actions,manage_pages";
    wp_redirect($dialog_url);
    die;

if(isset($_COOKIE['fbapp_session_state']) && isset($_REQUEST['state']) && ($_COOKIE['fbapp_session_state'] === $_REQUEST['state'])) :    
    $my_url=urlencode('http://it.thietkewebwp.info/settings/?act=fbapp_edit&id=1');
    $code = $_REQUEST["code"];
    $token_url = "https://graph.facebook.com/".FBAP_FB_API_VERSION."/oauth/access_token?"
    . "client_id=" . $fbapp_id . "&redirect_uri=" . $my_url
    . "&client_secret=" . $fbapp_secret . "&code=" . $code;

    $params = null;$access_token="";
    $response = wp_remote_get($token_url);

    if(is_array($response))
    
        if(isset($response['body']))
        
            parse_str($response['body'], $params);            
            
            if(isset($params['access_token']))$access_token = $params['access_token'];
        
    
    
    if($access_token!="")      
        $fields = array();
        $fields['fbapp_token'] = $access_token;
        $where = array('ID'=>$_GET['id']);
    
        $updated = $wpdb->update($wpdb->prefix.'fbapp', $fields, $where);
        // if($updated)
        //     wp_redirect(get_page_link('settings'));die;
        // 
    
endif;

if(isset($_POST['pubthoi']) && !empty($_POST['postLink_link']))
	// wp_delete_post(304);
	$fbapp_page = explode('-',$fbapp_pages);	
	$fb = new Facebook\Facebook([
	    'app_id' => $fbapp_id,
	    'app_secret' => $fbapp_secret,
	    'default_graph_version' => 'v2.4',
	    'default_access_token' => isset($fbapp_page[1]) ? $fbapp_page[1] : '1044300938965725|ca1da3267aca55b7b6c00494e9ac19c6'
	]);
	// echo '<pre>';
	// var_dump($_POST);
	// echo '</pre>';
	// die;
	$linkData = [
		'link' => $_POST['postLink_link'],		
		'message' => !empty($_POST['postLink_message']) ? $_POST['postLink_message'] : '',
		'name' => !empty($_POST['postLink_name']) ? $_POST['postLink_name'] : '',
		'caption' => !empty($_POST['postLink_caption']) ? $_POST['postLink_caption'] : "yan.vn | Bởi YAN News",
		'description' => !empty($_POST['postLink_description']) ? $_POST['postLink_description'] : '',
		'picture' => !empty($_POST['postLink_picture']) ? $_POST['postLink_picture'] : '',	
        // 'with_tags'=>'https://www.facebook.com/tuoitre.newspaper',
	];
	try 
   		$response = $fb->post('/'.$fbapp_page[0].'/feed', $linkData,$fbapp_page[1]);
	catch (Facebook\Exceptions\FacebookResponseException $e) 
   	// When Graph returns an error
   		echo 'Graph returned an error: ' . $e->getMessage();
   		exit;
	catch (Facebook\Exceptions\FacebookSDKException $e) 
   		// When validation fails or other local issues
   		echo 'Facebook SDK returned an error: ' . $e->getMessage();
   		exit;
	


?>
<form method='POST' action='' class="settings">
    <div class="row">
        <div class="col-sm-6">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Facebook Settings</h3>
                </div>
                <div class="panel-body">
                	<div class="form-group">
	                    <label for="">Fanpage</label>
	                    <input type="text" name="fbapp_name" class="form-control" placeholder="Facebook Fanpage" value="<?php echo $fbapp_name; ?>">
	                </div>
	                <div class="form-group">
	                    <label for="fbapp_id">Application id</label>
	                    <input type="text" name="fbapp_id" class="form-control" id="fbapp_id" placeholder="Facebook app ID" value="<?php echo $fbapp_id; ?>" />
	                </div>
	                <div class="form-group">
	                    <label for="fbapp_secret">Application secret</label>
	                    <input type="text" name="fbapp_secret" class="form-control" id="fbapp_secret" placeholder="Facebook app Secret" value="<?php echo $fbapp_secret; ?>"/>
	                </div>
	                <div class="form-group">
	                    <label for="fbapp_user">Facebook user id</label>
	                    <input type="text" name="fbapp_userid" class="form-control" id="fbapp_userid" placeholder="Facebook user id" value="<?php echo $fbapp_userid; ?>"/>
	               	</div>
                </div>
            </div>
        </div><!-- /.col-sm-6 -->
        <div class="col-sm-6">
        	<div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">Facebook Settings</h3>
                </div>
                <div class="panel-body">
                	<div class="form-group">
	                    <input type="submit" class="submit_fbap_new" name="fb_auth" value="Reauthorize" title="Reauthorize the account">
	                    <textarea class="form-control" name="fbapp_token" id="code_access" cols="30" rows="4"><?php echo $fbapp_token; ?></textarea>
	                </div>
                   	<div class="form-group">
                    <?php if(!empty($fbapp_token)): 
                        echo '<label for="">Select facebook pages</label>';
                        $offset = 0;
                        $limit = 100;
                        $data = array();
                        do 
                            $result1 = "";
                            $pagearray1 = "";
                            $pp = wp_remote_get("https://graph.facebook.com/" . FBAP_FB_API_VERSION . "/me/accounts?access_token=$fbapp_token&limit=$limit&offset=$offset");                      
                            if (is_array($pp)) 
                                $result1 = $pp['body'];
                                $pagearray1 = json_decode($result1);
                                if (is_array($pagearray1->data))
                                    $data = array_merge($data, $pagearray1->data);
                             else
                                break;
                            $offset += $limit;
                        while (isset($pagearray1->paging->next));
                        $count = count($data);
                        $fbap_pages_ids0 = array();
                        if ($fbapp_pages != "") $fbap_pages_ids0 = explode(",", $fbapp_pages);
                        $fbap_pages_ids = array();
                        for ($i = 0; $i < count($fbap_pages_ids0); $i++) 
                            if ($fbap_pages_ids0[$i] != "-1")
                                $fbap_pages_ids[$i] = trim(substr($fbap_pages_ids0[$i], 0, strpos($fbap_pages_ids0[$i], "-")));
                            else
                                $fbap_pages_ids[$i] = $fbap_pages_ids0[$i];
                        
                        ?>
                        <select class="form-control" name="fbapp_pages">
                            <option value="-1" <?php if (in_array(-1, $fbap_pages_ids)) echo "selected" ?>>Profile Page</option>
                            <?php for ($i = 0; $i < $count; $i++): ?>
                            <option value="<?php echo $data[$i]->id . "-" . $data[$i]->access_token; ?>" <?php if (in_array($data[$i]->id, $fbap_pages_ids)) echo "selected" ?>><?php echo $data[$i]->name; ?></option>
                            <?php endfor; ?>
                        </select>
                        <?php endif; ?>
                    </div>
                    <div class="form-group">
	                    <label for="fbapp_page_crawler">Page Get Data</label>
	                    <input type="text" name="fbapp_page_crawler" class="form-control" id="fbapp_page_crawler" placeholder="Facebook Fanpage Crawler" value="<?php echo $fbapp_page_crawler; ?>" />
	                </div>
                    <label for="">Loại tin cần lấy</label>
                    <div class="form-group">
                    	<label class="checkbox-inline">
                    		<input <?php if($fbapp_get_type=='all') echo 'checked=checked'; ?> type="checkbox" name="fbapp_get_type" value="all"> Tất cả
                    	</label>
                    	<label class="checkbox-inline">
                    		<input <?php if($fbapp_get_type=='video') echo 'checked=checked'; ?> type="checkbox" name="fbapp_get_type" value="video"> Video
                    	</label>
                    	<label class="checkbox-inline">
                    		<input <?php if($fbapp_get_type=='photo') echo 'checked=checked'; ?> type="checkbox" name="fbapp_get_type" value="photo"> Photo
                    	</label>
                    	<label class="checkbox-inline">
                    		<input <?php if($fbapp_get_type=='status') echo 'checked=checked'; ?> type="checkbox" name="fbapp_get_type" value="status"> Status
                    	</label>
                    	<label class="checkbox-inline">
                    		<input <?php if($fbapp_get_type=='link') echo 'checked=checked'; ?> type="checkbox" name="fbapp_get_type" value="link"> Gif
                    	</label>
                    </div>

                </div>
            </div>
            
        </div>
    </div>
    <div class="row">
	    <div class="col-sm-6">
	    	<div class="panel panel-default">
		    		<div class="panel-body">
				<input type="hidden" name="postType" id="postType" value="link">
					<div class="formField">
						<label for="message">Message <a href="#" data-toggle="tooltip" data-placement="top" style="float:right" title="" data-original-title="Spinning example : Hello|Howdy|Hola to you, Mr.|Mrs.|Ms. foo|bar|foobar!!"><span class="glyphicon glyphicon-question-sign"></span></a></label>
						<textarea name="message" id="message" rows="3" cols="50" class="form-control" placeholder="Your status here..."></textarea>
						
					</div>

					<div id="postLinkDetails" style="">
						<div class="formField">
							<label for="link">Link</label>
							<input type="text" name="postLink_link" id="link" class="form-control" value="" placeholder="Post link here.">
						</div>
						<div class="formField">
							<label for="picture">Picture</label>
							<input type="text" name="postLink_picture" id="picture" class="form-control" value="" placeholder="Post picture here.">
						</div>
						<div class="formField">
							<label for="name">Name</label>
							<input type="text" id="name" name="postLink_name" class="form-control" value="" placeholder="Post name here.">
						</div>
						<div class="formField">
							<label for="caption">Caption</label>
							<input type="text" name="postLink_caption" id="caption" class="form-control" value="" placeholder="Post Caption here.">
						</div>
						<div class="formField">
							<label for="description">Description</label>
							<textarea name="postLink_description" id="description" rows="3" cols="50" class="form-control" placeholder="Post description here."></textarea>
						</div>
					</div>
					<br>
					<div class="formField">
						<button type="submit" class="btn btn-primary" id="post" name="pubthoi">
							<span class="glyphicon glyphicon-send"></span> Send 
						</button>
						<span class="messageBox"></span>
					</div>
				</div>
	    	</div>
	    </div>
	</div>
    <button type="submit" name="pubthoi">Pub thoi</button>
    <button type="submit" name="submit_page">Submit</button>
    <input type="submit" name="save_record" value="Save change" class="btn btn-primary" />
</form>

csharp代码片段(代码片段)

查看详情

javascript代码片段(代码片段)

查看详情

textvisualbasic代码片段(代码片段)

查看详情

sqloracle代码片段(代码片段)

查看详情

swift代码片段(代码片段)

查看详情

java代码片段【安卓】(代码片段)

查看详情

shbash的代码片段(代码片段)

查看详情

markdownphpexcelnotes和代码片段(代码片段)

查看详情

javaandroid的代码片段(代码片段)

查看详情

javascriptjs-常用代码片段(代码片段)

查看详情

常用代码片段(代码片段)

单例模式privatestaticHttpUtilinstance;publicstaticsynchronizedHttpUtilgetInstance()if(instance==null)instance=newHttpUtil();returninstance; 查看详情

常用代码片段(代码片段)

单例模式privatestaticHttpUtilinstance;publicstaticsynchronizedHttpUtilgetInstance()if(instance==null)instance=newHttpUtil();returninstance; 查看详情

text代码片段很有用(代码片段)

查看详情

vbscript我的代码片段(代码片段)

查看详情

java代码片段【java】(代码片段)

查看详情

rr有用的代码片段(代码片段)

查看详情

常见的代码片段(代码片段)

$(id).select2(placeholder:"--请选择--",allowClear:true,data:list);  查看详情

text代码片段【snl】(代码片段)

查看详情