Jump to content

facebook


Manol
 Share

Recommended Posts

Hello.

     I wish to publish some processwire pages to facebook, including images. I got it working thanks to adrian and Martijn using ifttt, but I would like to use my own code and publish images or other stuff through the facebook php sdk.

    At the moment  I've created a simple php page in my localhost that works, here is the code

<?php
 include_once 'inc/facebook.php';

 $appId = '703738********';
 $secret = 'd30530a5dcd*************';
 $returnurl = 'http://myweb.es/facebook/post.php';
 $permissions = 'manage_pages, publish_stream';
 
 $fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
 
 $fbuser = $fb->getUser();
 
 if($fbuser){
		

        try{
            $message = array(
                'message' => 'foo'
            );

            $result = $fb->api('/me/feed/','POST',$message);
            if($result){
                echo 'Published';
            }
        }catch(FacebookApiException $e){
            echo $e->getMessage();
        }

    
 }else{
    $fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
    echo '<a href="'.$fbloginurl.'">Login with Facebook</a>';
 }
 
?>

but when I try it  on a pw template or module it doesn't work

$this->pages->addHookAfter('save', $this, 'fb'); 

.....		

if($page->facebook==1) {

			 include_once 'fb/facebook.php';

			 $appId = '70373825******';
			 $secret = 'd30530a5dcd4e5829e6c8b********';
			 // $returnurl = 'http://indinet.es/facebook/post.php';
			 $returnurl = 'http://pwpage.com';
			 $permissions = 'manage_pages, publish_stream';
			 $fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
			 
			 $fbuser = $fb->getUser();

			 $this->message("user " . $fbuser);
 
		} // facebook
....

any ideas?

  • Like 1
Link to comment
Share on other sites

Hi Nico

   I deleted some bits of the code, but even that bit doesn't  get the user id, it just gets a 0

<?php

/**
 * ProcessWire 'Hello world' demonstration module
 *
 * Demonstrates the Module interface and how to add hooks.
 * 
 * ProcessWire 2.x 
 * Copyright (C) 2010 by Ryan Cramer 
 * Licensed under GNU/GPL v2, see LICENSE.TXT
 * 
 * http://www.processwire.com
 * http://www.ryancramer.com
 *
 */

class FaceMail extends WireData implements Module {

	/**
	 * getModuleInfo is a module required by all modules to tell ProcessWire about them
	 *
	 * @return array
	 *
	 */
	public static function getModuleInfo() {

		return array(

			// The module'ss title, typically a little more descriptive than the class name
			'title' => 'Facebook Email Publish', 

			// version: major, minor, revision, i.e. 100 = 1.0.0
			'version' => 101, 

			// summary is brief description of what this module is
			'summary' => 'Publica noticias en Facebook y envia emails masivos',
			
			// Optional URL to more information about the module
			'href' => 'http://www.processwire.com',

			// singular=true: indicates that only one instance of the module is allowed.
			// This is usually what you want for modules that attach hooks. 
			'singular' => true, 

			// autoload=true: indicates the module should be started with ProcessWire.
			// This is necessary for any modules that attach runtime hooks, otherwise those
			// hooks won't get attached unless some other code calls the module on it's own.
			// Note that autoload modules are almost always also 'singular' (seen above).
			'autoload' => true, 
			);
	}

	/**
	 * Initialize the module
	 *
	 * ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
	 * when ProcessWire's API is ready. As a result, this is a good place to attach hooks. 
	 *
	 */
	public function init() {

		// add a hook after the $pages->save, to issue a notice every time a page is saved
		$this->pages->addHookAfter('save', $this, 'publicaEnvia'); 

	}

	/**
	 * Example1 hooks into the pages->save method and displays a notice every time a page is saved
	 *
	 */
	public function publicaEnvia($event) {

		$page = $event->arguments[0]; 

		/**
		 * 1.- sino es la pagina de noticia -> salir
		 */
		if($page->template != 'noticia') return; 

		
		/**
		 * 2.- si esta check facebook -> publicalo
		 * @var [type]
		 */
		if($page->facebook==1) {

			 include_once 'fb/facebook.php';

			 $appId = '70373825635****';
			 $secret = 'd30530a5dcd4e5829e6c8bc8ba72****';
			 // $returnurl = 'http://indinet.es/facebook/post.php';
			 $returnurl = 'http://casergi.com';
			 $permissions = 'manage_pages, publish_stream';
			 $fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
			 
			 $fbuser = $fb->getUser();

			 $this->message("user " . $fbuser);
 
		} // facebook

		/**
		 * 3.- se esta check enviar email a clientes -> envia correos
		 * @var [type]
		 */
		if($page->enviar_a_Clientes==1) {

			$usuarios = wire(pages)->get("/acceso/access/users/");

				$emails = $usuarios->children(); 
				foreach ($emails as $email) {
					if($email->email) $this->message("enviado a: ". $email->email. ", ");
					mail($email->email, $page->title, $page->publicidad, "From: cassergi.com\nContent-Type: text/html");
				}	

			$this->message("enviado por correo a los clientes");
		}

	}


	
}
 
Link to comment
Share on other sites

  • 6 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...