index of $_POST seemingly empties at random - php

Ok so this is driving me insane! I've got a script written that takes user submitted product information from a previous page and sends it to the user's facebook business page via the facebook php sdk.
Here's the php code:
$app_id = "1234567890";
$app_secret = "1234567890";
$page_id = "1234567890";
$page_token = "1034921234567890";
$store_name = "MyStore.com";
$store_url = 'http://www.mystore.com';
$cur = '$';
$new_message = "New Product!";
$prod_image = $store_url . "/images/" . $_POST['products_image'];
$price = $products_price;
$prod_url = $store_url . '/index.php?main_page=product_info&cPath=' . $current_category_id . '&products_id=' . $products_id;
$prod_name = $_POST['products_name'][1];
$prod_description = $_POST['products_description'][1];
include_once 'facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true, ));
$attachment = array(
'access_token' => $page_token,
'message' => $new_message,
'name' => $prod_name,
'link' => $prod_url,
'caption' => 'Price: '.$cur . $price,
'description' => $prod_description,
'picture' => $prod_image
);
Everything sends to facebook fine except on some servers$prod_description aka $_POST['products_description'][1] ends up empty. When a var_dump($_POST['products_description']); is placed on the same line, it comes back with:
array(1) { [1] => string(35) "This is a
test product description." }
So I know the information is there and not being overwritten it's just for whatever reason when sent to facebook (with cURL from the include_once file) it doesn't post.
I should also say that this is a Zen Cart mod that is available for download and I while haven't been able to reproduce the error myself (test on 4 different servers) I have had miltiple users add the var_dump and send me the results.
I guess my question is; Is there something (like a ini setting, cURL option, etc) that may vary from server to server that would cause random indexes to empty when sent via cURL and is there something I can do to fix it?
Thanks in advance.

I think problem is in the way data is passed. You have spaces in the description, that can cause problems...
Try explicitly setting enctype for the form to "application/x-www-form-urlencoded"
<form enctype="application/x-www-form-urlencoded" .... >
You can also try to url_encode the product description before passing it to Facebook.
$prod_description = urlencode($_POST['products_description'][1]);

Related

Post picture to my Facebook page without link

I am writing a script to automatically retrieve a .png file from a directory and post it to my Facebook page with a bunch of hashtags as its message.function
publish($filename, $card_type, $message){
$filename = "http://www.mywebsite.com/sandboxassets/img/burrocards/" . $card_type . "/" . $filename;
echo 'publish this file: ' . $filename;
// retrieve fb credentials
$config_vals = parse_ini_file("../../../nicethings.ini");
$appid = $config_vals['fbappid'];
$token = $config_vals['fbtoken'];
$secret = $config_vals['fbsecret'];
// initialize Facebook class using your own Facebook App credentials
$fb = new Facebook\Facebook([
'app_id' => $appid,
'app_secret' => $secret,
'default_graph_version' => 'v2.8',
]);
// define your POST parameters (replace with your own values)
$params = array(
"access_token" => $token,
"message" => $message,
"link" => "http://www.mywebsite.com",
"picture" => $filename
);
// post to Facebook
try {
$post = $fb->post('/MyPage/feed', $params);
$post = $post->getGraphNode()->asArray();
echo 'Successfully posted to Facebook';
} catch(Exception $e) { echo $e->getMessage(); }
}
This script runs just fine except that I don't want to link the posted image to any site. I just need to post it as an unlinked image. But when I remove the following line from the function, Facebook refused the call saying I must define a link for an image post:
"link" => "http://www.mywebsite.com",
Is there any way to accomplish what I want to? There must be because I regularly post images to my page manually without having to link it to anything.

Facebook API - Subscriptions Timeout in callback

I have a really strange problem in PHP with the Facebook API. All this code is working in my testing server (shared hosting). But when I run it in the production server (Heroku) I get an error.
What I am trying to do is to receive realtime updates from Facebook when my app users likes (or unlikes) a new page.
This is the documentation that Facebook provides: https://developers.facebook.com/docs/graph-api/real-time-updates
My code to subscribe:
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$facebook->api(
"/" . $app_id . "/subscriptions", "POST", array(
'object' => 'user',
'callback_url' => 'http://' . $url . '/rtu/callback',
'fields' => 'likes',
'verify_token' => $rtu_verify_token,
'access_token' => $app_id . "|" . $app_secret
)
);
My code in the callback:
if (isset($_GET['hub_verify_token']) && isset($_GET['hub_challenge'])) {
$rtu_verify_token = Config::get('variables.rtu_verify_token');
if (($_GET['hub_verify_token'] == $rtu_verify_token)) {
echo $_GET['hub_challenge'];
die();
}
}
$data = file_get_contents("php://input");
$json = json_decode($data);
The error:
FacebookApiException (#2200) callback verification failed: Operation
timed out after 6001 milliseconds with 0 bytes received
I have already verify the app_id, url and the app_secret variables.
Again, this works in my testing server, but not in Heroku.
I even tried not to use the Facebook API and make a cURL call, but I got the same error.
I'm thinking that maybe has something to do with Heroku, but i couldn't find any evidence.
Thanks
Your server must be reachable from the internet, and be able to process concurrent requests, because facebook sends verification requests simultaneously.

Post on facebook page by cron

I have read all the postings related to the title. But I have a very simple question. It will be very helpful if anyone can answer that.
What is me/account page in facebook?
I got many postings here such as
Posting to facebook company page with cron php server side
Everything is explained, but I know this sounds funny, but I stuck in the easiest step.
"When using Graph Explorer you would be required to navigate to /me/accounts end point,"
Please help me.
Please check the code
include 'includes/facebook.php';
$app_id = "XXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXXX";
$page_id = "XXXXXXXXXXXX";
$my_url = "http://XXXXXXXXXXX.com";
$page_access_token = "XXXXXXXXXXXXXXX";
//Create the facebook object
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
//Write to the Page wall
try {
$attachment = array(
'access_token' => $page_access_token,
'message'=> "Hello World"
);
$result = $facebook->api('/639386542780904/feed', 'post', $attachment);
} catch(Exception $e) {
echo "error";
// ...
// mail($to, $subject, $message, $headers);
}
me/accounts is used to get a facebook page acces token, so that your app will post as that page. Elsewhere, your app will post as you on that page.

Slow script...want to display iframe

I have a php script that is slow, but must complete in it's entirety. At the end of the script it redirects to a url.
I am thinking of somehow displaying an iframe while the scirpt is loading so visitors don't leave the page. What is the best way of doing this? With the iframe there would be no need to redirect after the completion of the script.
<?php
require_once('config.php');
if(!$_SESSION['init']){
die;
}
unset($_SESSION['init']);
require_once('facebook.php');
$data = loaddata();
$facebook = new Facebook(array(
'appId' => $fbappid,
'secret' => $appsecret,
'cookie' => true,
));
$friendsdate = $facebook->api('/me/friends');
if($friendsdate && $friendsdate['data'] && count($friendsdate['data'])){
$friends = array();
foreach($friendsdate['data'] as $f){
$friends[] = $f['id'];
}
sleep(3);
$session = $facebook->getSession();
$params = array('name' => $data['name'],
'start_time' => $data['start_time'],
'end_time' => $data['end_time'],
'description' => $data['description']);
if($data['source']){
$params['source'] = '#'.realpath('image/'.$data['source']);
$facebook->setFileUploadSupport(true);
}
$result = $facebook->api('/me/events', 'POST', $params);
sleep(4);
$eid = $result['id'];
$params = array(
'access_token' => $facebook-> getAccessToken(),
'eid' =>$eid,
'api_key' => $fbappid,
'uids'=> implode(',', $friends),
'format'=>'json-strings',
'personal_message'=> $data['personal_message']
);
$url = 'https://api.facebook.com/method/events.invite';
$facebook->setFileUploadSupport(false);
$result = $facebook->makeRequest($url, $params);
}
header('Location: '.$data['url']);
The proper way to do what you want is to use ajax.
Main page:
Shows some sort of "working" message in a div.
On load, javascript calls out to the worker page.
On response, the javascript replaces the "working" div with the response from the worker page.
Worker page:
Does your long php work.
Displays whatever you think is appropriate.
Here is a tutorial on AJAX to get you started: http://www.w3schools.com/ajax/default.asp
Yes it'll work, facebook api gets very slow at times, so you can use iframe or ajax.
For iframe - just replace the final header('location:... with javascript redirect
echo "<script>self.parent.location = 'http:// ... ';</script>";
Also you must do session_commit somewhere at the script's top, if you're using php sessions because otherwise the iframe could lock the main script. AJAX... not really necessary.

Facebook stream.publish and outdated template bundles?

I haven't tried messing with the facebook PHP API for months. Since template bundles are apparently now defunct, how can I publish a story into my users news feed for their friends? I've also already requested permissions.
Edit: The issue seems to arise from requested permissions not being set for the user when They are granted.
So far I have this
$appapikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$appsecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$facebook = new Facebook($appapikey, $appsecret);
$fb_user = $facebook->require_login();
try {
$facebook->api_client->feed_publishUserAction();
} catch(Exception $e) { }
Edit: I've looked through the facebook "api documentation" multiple times it's just not clear to me. I can't tell what's actually deprecated or not. They link to tutorials 2-3 years old!
If you have a problem with your iframe application reloading over and over and over try using
$facebook->require_frame()
Have you already looked up the topic on the facebook wiki? http://wiki.developers.facebook.com/index.php/Stream.publish
There's a nice example which should help you out. If not, you'll have to describe your problem more accurate.
EDIT: You can check and request for the permissions like this (and also request them)
function check_perms() {
global $facebook, $uid;
$data = $facebook->api_client->fql_query( "SELECT uid, publish_stream FROM permissions WHERE uid = " . $uid );
if( $data[0]['publish_stream'] != true ) {
echo '<br /><p>No \'publish_stream\' permissons found!<br />';
echo '<fb:prompt-permission perms="publish_stream"> Allow me to publish to your wall (*click*) </fb:prompt-permission>';
echo '<br />You\'ll have to refresh the page to continue.</p>';
die();
}
}
<?php
$message ="Your Message";
$attachment = array(
'name' => 'Application Name or message',
'href' => 'http://apps.facebook.com/tshirtquote',
'description' => 'Choose/Write your T-shirt Quote, Get A Tshirt Free printed with your Favorite Quote',
'media' => array(array('type' => 'image', 'src' => 'http://linkdoo.com/tshirtquote/images/tshirt1.JPG', 'href' => 'http://apps.facebook.com/tshirtquote/')),
);
$action_links = array( array('text' => 'WriteYourTShirtQuote', 'href' => 'http://apps.facebook.com/tshirtquote'));
$attachment = json_encode($attachment);
$action_links = json_encode($action_links);
$message = json_encode($message);
?>
<script>
var attachment = <?= $attachment ?>;
var message = <?= $message ?>;
var action_links = <?= $action_links ?>;
Facebook.streamPublish(message,attachment,action_links);
</script>
Use above script , It is most easy way to publish

Categories