facebook app losing session values after redirection - session

I am using Facebook PHP SDK 3.1.1
This page is supposed to collect value from header and later use it for uploading images.
Problem: $_SESSION['file'] becomes null after user logs in to Facebook.
<?php
require 'facebook.php';
include('connect.php');
$facebook = new Facebook(array('appId' => "XXXXXX",'secret' => "XXXXX","cookie" => true,'fileUpload' => true));
session_start();
$user_id = $facebook->getUser();
if(isset($_REQUEST["src"]))
{
echo "<center><img src='loader.gif'/></center>";
echo "<center><div id='msg'>Exporting Image..</div></center>";
$_SESSION['file'] = $_REQUEST["src"];
$_SESSION['club'] = $_REQUEST["club"];
}
$code = $_REQUEST["code"];
if(empty($code))
{
$_SESSION['state'] = md5(uniqid(rand(), TRUE));
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=". $app_id . "&redirect_uri=" . urlencode($my_url) . "&state=". $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
exit();
}
$token_url = "https://graph.facebook.com/oauth/access_token?". "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url). "&client_secret=" . $app_secret . "&scope=user_photos,email,read_stream,publish_stream&code=".$code;
function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$response = url_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token=". $params['access_token'];
$user = json_decode(url_get_contents('https://graph.facebook.com/me/albums?access_token='.$params['access_token']),true);
foreach($user as $key=>$value)
{
foreach($user[$key] as $key1=>$value1)
{
if($value1['name'] == "Photos")
{
$album_uid = $value1['id'];
}
}
}
if(!isset($album_uid))
{
echo 'Create an album';
$album_details = array(
'message'=> 'Uploaded via XXX',
'name'=> 'Photos'
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
$album_uid = $create_album['id'];
}
$photo_details = array('message'=>$_SESSION["club"]);
try{
$file = NULL;
$file="../Photos/".$_SESSION['file'].".jpeg";
echo '<script>document.getElementById("msg").innerHTML = "Uploading Image"</script>';
$photo_details['image'] = '#' . realpath($file);
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);
}catch(Exception $e){
echo "<script type='text/javascript'>top.location.href = 'http://www.facebook.com/PAGE_URL/';</script>";
exit();
}
$_SESSION['file'] is null
Am I doing it right ? or something is wrong with my web hosting ?

The problem is that your app is running inside of an IFRAME and the session cookies are not being preserved. Please see here:
Facebook Iframe App with multiple pages in Safari Session Variables not persisting

Related

Automatically replace the first & with a ? question mark

I have this code below which generates a query string for my url.
Question I would like to know is there away that it could automatically replace the first & with a ? question mark no matter what the first $_GET is.
$url = '';
if ($this->input->get('directory')) {
$pos = strrpos($this->input->get('directory'), '/');
if ($pos) {
$url .= '&directory=' . urlencode(substr($this->input->get('directory'), 0, $pos));
}
}
if ($this->input->get('target')) {
$url .= '&target=' . $this->input->get('target');
}
if ($this->input->get('thumb')) {
$url .= '&thumb=' . $this->input->get('thumb');
}
$data['parent'] = base_url('image_manager' . $url);
Found solution
$find = '&';
$replace = '?';
$result = preg_replace("/$find/", $replace, $url, 1);
echo $result;

Magento 2 oauth 1 get request token

Am not able to get request token in magento app. Signature is always invalid.
This is the code i use to generate the signature. The consumer key is the one is i get from localhost:8000 (magento shop) request to localhost:3000.
I tried changing the url both to localhost 8000 and 3000.
Followed these instructions: Magento instructions
function getSignature($consumerKey)
{
$params = array(
'oauth_nonce' => uniqid(mt_rand(1, 1000)),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_version' => '1.0',
'oauth_consumer_key' => $consumerKey,
);
ksort($params);
$baseString = strtoupper('POST') . '&';
$baseString .= rawurlencode('http://localhost:8000') . '&';
$baseString .= rawurlencode(buildSignatureDataString($params));
$signature = hash_hmac('SHA1', $baseString, getSigningKey(), true);
return base64_encode($signature);
}
function getSigningKey()
{
return rawurlencode('magento_private_key') . '&';
}
function buildSignatureDataString(array $signatureData)
{
$signatureString = '';
$delimiter = '';
foreach ($signatureData as $key => $value) {
$signatureString .= $delimiter . $key . '=' . $value;
$delimiter = '&';
}
return $signatureString;
}

Receive external data - Magento

First I want to say that this text was translated by Google Translate ( Excuses !).
How do I send data from an external server for my Magento store?
I looked hard for it today and found nothing basically want another system sends a notification to my Magento store and change the status of my order , I have not found a way to receiving external data in my store.
If you already have a related question I apologize.
Edit
This is my external file that sends the data:
$fields = '';
foreach($data as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, 1);
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
var_dump($fields);
try {
$result = curl_exec($post);
$info = curl_getinfo($post);
echo curl_getinfo($post, CURLINFO_HTTP_CODE);
echo "<br>";
echo var_dump($info);
} catch (\Exception $e) {
$result = $e->getMessage();
echo $e;
}
}
$data = array(
"name" => "c.bavota",
"website" => "http://bavotasan.com",
"twitterID" => "bavotasan"
);
post_to_url("http://localhost/magento/safe/teste/mostra/", $data);
?>
This is my controller in Magento:
<?php
class Name_MyModule_TesteController extends Mage_Core_Controller_Front_Action{
public function mostraAction(){
$teste = $this->getRequest()->getPost('name');
echo 'Olá mundo';
echo '<br>Teste: '.isset($teste);
}
}
ps: I'm testing with locally with WAMP

Image not show in browser with Lumen Api

I use Lumen to get images and show in browser, my code in controller is :
use Illuminate\Support\Facades\File;
$photo = $this->uploadFile->get_by_photo($photo, ['filename']);
$path = storage_path('app') . '/' . $photo[0]['filename'];
$file = File::get($path);
$type = File::mimeType($path);
$response = response()->make($file, 200);
$response->header("Content-Type", $type);
return $response;
But image not show in browser , i just got a dark page when run that api
this code works:
$photo = $this->uploadFile->get_by_photo($photo, ['filename']);
$path = storage_path('app') . '/' . $photo[0]['filename'];
$type = File::mimeType($path);
$headers = array('Content-Type' => $type);
$response = response()->download($path, $photo, $headers);
ob_end_clean();
return $response;

Importing/parsing XML from URL into Magento

I have a URL, which contains XML of products.
What I'm looking to do, is, if possible, parse the contents and then import the details as products into Magento.
Is this possible and If so, what would be the steps I need to do to carry this out?
Thanks
EDIT:
import_products.php
require_once('app/Mage.php');
class import_products extends Mage_Core_Model_Abstract
{
public static function url_contents($url)
{
$crl = curl_init();
$timeout = 5;
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
$ret = curl_exec($crl);
curl_close($crl);
return $ret;
}
}
import.php
require_once('import_products.php');
$app = Mage::app('default');
$product = Mage::getSingleton('catalog/product');
$objDOM = new DOMDocument();
$objDOM->load("http://www.example.com/products.xml");
$note = $objDOM->getElementsByTagName("car");
// for each note tag, parse the document and get values for
// tasks and details tag.
foreach( $note as $value )
{
$car_ids = $value->getElementsByTagName("Car_ID");
$car_id = $car_ids->item(0)->nodeValue;
$makes = $value->getElementsByTagName("Make");
$make = $makes->item(0)->nodeValue;
$models = $value->getElementsByTagName("Model");
$model = $models->item(0)->nodeValue;
$descriptions = $value->getElementsByTagName("Description");
$description = $descriptions->item(0)->nodeValue;
$short_descriptions = $value->getElementsByTagName("Specification");
$short_description = $short_descriptions->item(0)->nodeValue;
$prices = $value->getElementsByTagName("Price");
$price = $prices->item(0)->nodeValue;
$simple = 'simple';
$product->setAttributeSetId(4);
$product->setSku($task);
$product->setName($detail);
$product->setTypeId($simple);
$product->setPrice($price);
$product->setValue($price);
$product->setDescription($description);
$product->setShortDescription($short_description);
$product->setWeight(100);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
$product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$product->setTaxClassId(2);
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 99999
));
try {
$product->save();
echo "Saved";
}
catch (Exception $ex) {
echo "<pre>".$ex."</pre>";
}
//echo "$car_id :: $make :: $model :: $description :: $short_description :: $price <br /><br />";
}
you could get the parameter in Magento and use simple xml to turn the string into xml
$xmlstr = self::XML_DECLARATION . urldecode($this->getRequest()->getParam('paramName');
try {
$xml = simplexml_load_string($xmlstr);
}
catch(Exception $ex) {
echo "Unable to process xml file because of the following error<br /><br /> $ex";
exit;
}
if it is a file you are trying to get you can setup a php file with the following code in it to get the contents of the url
function get_url_contents($url){
$crl = curl_init();
$timeout = 5;
curl_setopt ($crl, CURLOPT_URL,$url);
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
$ret = curl_exec($crl);
curl_close($crl);
return $ret;
}
You can use that function and then where in the above we had
$xmlstr = self::XML_DECLARATION . urldecode($this->getRequest()->getParam('paramName');
You should be able to do this
$xmlstr = self::XML_DECLARATION . get_url_contents('http://urlhere.com/xml');
Then you could use simple xml and assign the nodes to a products array, where you could have a collection of products and then loop through those and programmatically create the products in Magento with something like this, obviously change the values that might be hard coded for whatever you might need.
$product = Mage::getSingleton('catalog/product');
// Build the product
$product->setSku($productData['sku']);
$product->setAttributeSetId(26);
$product->setTypeId('simple');
$product->setName($productData['description']);
$product->setCategoryIds(array(162));
$product->setWebsiteIDs(array(1));
$product->setDescription($productData['description']);
$product->setShortDescription($productData['description']);
$product->setPrice($productData['price']); # Set some price
$product->setWeight(4.0000);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
$product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$product->setData('is_salable', '1');
$product->setTaxClassId(2); # My default tax class
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 99999
));
try {
$product->save();
}
catch (Exception $ex) {
//Handle the error
}

Resources