Facebook SDK - publish post with external image - php

I am having hard time publishing external image to post. I am using PHP language and curl. I would like to post image (e.g. https://cdn.pixabay.com/photo/2015/06/19/17/58/sample-815141_1280.jpg) to facebook, but the only thing which get's posted is message without any image.
The problem with using photo api is that the post is posted as a picture and not as a post containing image. How can I accomplish this? I also tried with official PHP SDK but so far no luck.
$data['message'] = $_POST["content"];
$data['access_token'] = $page_access_token;
$post_url = 'https://graph.facebook.com/'.$page_id.'/feed';
if($_POST["picture"]!==""){
$data_post['url']=$_POST["picture"];
}
if($_POST["content"]!=="")
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
}

Related

URL keeps on loading which is executed using php curl

I am working on academic project for ecommerce which is about online book renting using PHP. I am making sure that user can only view rented book pdf file only after visiting site to do so I have use cloudfile.io which generate link for sharing file. I can use embed tag for adding file to my page but I don't want user to see file link so I use curl function to display file in my page but it keeps on loading.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://cloudfil.es/0mU5B5e0eX3");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
echo $data;
curl_close($ch);
?>
I have tried to use headers for displaying pdf file this also doesn't work.
You need to clear anything already sent to the browser then send the correct headers so that the browser knows that a PDF is coming.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://cloudfil.es/0mU5B5e0eX3');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
curl_close($ch);
while (ob_get_level() !== 0) {ob_end_clean();}
header('Content-type:application/pdf');
header('Content-Disposition:attachment;filename="YourFileNameHere.pdf"');
echo $data;

How can i upload and post an image to linkedin using api?

I'm trying to create an app for posting images to linkedin. Linkedin has an api which accepts an image url and a message, but it seems to be not the exact way to post an image since it is showing my image as a link instead of an image. But in the case of Buffer App, it displays full image, not the URL. Is there any way to post images to linkedin via api. Anyone please help me to sort this out.
Use Linkedin V2 API.Below code will upload the image.
curl -i --upload-file /Users/peter/Desktop/superneatimage.png --header "Authorization: Bearer redacted" 'https://api.linkedin.com/mediaUpload/C5522AQGTYER3k3ByHQ/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQJbrN86Zm265gAAAWemyz2pxPSgONtBiZdchrgG872QltnfYjnMdb2j3A&app=1953784&sync=0&v=beta&ut=2H-IhpbfXrRow1'
I suggest you to use Guzzle HTTP client to exicute this in your PHP application
This works for me
$upUrl = "Your Url";
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $upUrl);
curl_setopt($cURL, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array('Content-Type: application/binary'));
$path = "./uploads/image.jpg";
$file = fopen($path, 'r');
$size = filesize($path);
$fildata = fread($file, $size);
curl_setopt($cURL, CURLOPT_POSTFIELDS, $fildata);
curl_setopt($cURL, CURLOPT_INFILE, $file);
curl_setopt($cURL, CURLOPT_INFILESIZE, $size);
$response = curl_exec($cURL);
curl_close($cURL);

Delete pictures from Facebook using CURL

I have a Facebook app, where I want to delete some pictures. I have the pictureid and access_token, and have tried every methods I have found on this site, but I get errors every time.
How can this be deleted with CURL? I've started with this:
$ch = curl_init();
$url = 'https://graph.facebook.com/'.$uid.'/photos?access_token='.$access_token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
echo $data = curl_exec($ch);
Applications can now delete Photos - created by that application.
I tried it with a photo uploaded to a page and deleted it with its id via the Graph API Explorer
The curl url would be:
https://graph.facebook.com/'.$imageId.'?method=DELETE&access_token='.$token
Facebook does not permit applications to delete photos at all.

Is there a Facebook API to publish content to the FaceBook page using PHP?

I have a travel site and whenever I publish a new offer, I want to automatically update our sites FaceBook page.
Is there an API I can use for this?
Are there any recommended tutorials showing how to do this?
Our content is published with PHP.
Yes there are many ways for doing the same.
The most common one is to create a Facebook App for your Website.
https://developers.facebook.com/apps
Download the Facebook Development Kit from this URL based on the language you are using for development.
https://developers.facebook.com/docs/sdks/
This SDK will have the required Classes and functions call to help you submit contents from your web-page to your Facebook Page.
Go Ahead and have a look.
Try this example
$data = array(
"link" => $link,
"access_token" => $pages,
"picture" => $image,
"name"=> $title,
"description"=> $description
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/".$pageID."/feed");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$op = curl_exec($ch);
if (!$op) {
echo "Curl Error : ".curl_error($ch);
curl_close($ch);
exit;
}
curl_close($ch);
$res = get_object_vars(json_decode((string)$op));
print_r($res);

Malformed access token

In my application I am using facebook accesstoken to post a message to users fan page wall. Now while posting am getting an error like
Malformed access token
I am able to read the posts from fan page wall. But I cant post to wall. I am using 'Curl' to post.
Here is my code:
<!-- language: lang-php -->
$post_url = 'https://graph.facebook.com/pageid/feed';
$data['access_token'] = 'XYZXYZXYZXYZXYZXYZXYZ ';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($ch);
curl_close($ch);
Check if your database doesn't cutoff the access_token string when saved.
I had this problem with facebook once. Are you using a varchar(xx)?

Categories