I've serached a little but I didn't find anything that could solve my problem.
I've managed to post on page as page admin, link is added correctly, but image isn't attached. My code:
Blahblah authentication, etc.
$attachment = array(
'message' => 'text',
'name' => 'Name',
'link' => 'http://somelink.com',
'description' => '',
'access_token' => $ACCESS_TOKEN
);
if(something) $attachment['media'] = array(array('type'=>'image', 'src'=>'http://'.$_SERVER['HTTP_HOST']."/media/file/image_by_id/".$data['thumb_file_tree_id'].'/?w=400&h=500', 'href'=>'http://somelink.com'));
try {
if($facebook->api('/XXX/feed', 'post', $attachment))
{
echo 'Hooray, ok';
}
} catch (FacebookApiException $e) {
echo 'Damn';
}
Result: Correctly posted news with link ('Hooray, ok'), but without image. Can it be due to PHP generated image, and not png/jpg/gif extension? I've added some additional headers like etag, accepted-ranges, last modified, etc. What am I doing wrong (again)?
'Something' is true, checked with print_r whole attachment array.
E: Again removed [0]
To just attach an image, you can use the picture key in your attachment.
$attachment = array(
'message' => 'text',
'name' => 'Name',
'link' => 'http://somelink.com',
'description' => '',
'access_token' => $ACCESS_TOKEN,
'picture' => 'http://example.com/example.jpg'
);
Related
I'm writing API Client, but I can't send files via pecl_http. I wrote everything on http\Client. Most things by copy Postman things, but when I send I get null files. How should I send it? how I should put into this script my $_FILES variable with data?
<?php
$client = new http\Client;
$request = new http\Client\Request;
$body = new http\Message\Body;
$body->addForm(NULL, array(
array(
'name' => 'photo',
'type' => null,
'file' => 'user_path/2018-11-09 o 15.00.48.png',
'data' => null
)
));
$request->setRequestUrl('url');
$request->setRequestMethod('POST');
$request->setBody($body);
$request->setHeaders(array(
'Postman-Token' => 'f6154fff-46f4-47d0-a7c3-98d7de8d0f24',
'Cache-Control' => 'no-cache',
'Content-Type' => 'application/x-www-form-urlencoded'
));
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();
Here is part of the code I found in: Pecl_http Tutorial
<?php
$r = new HttpRequest('http://dev.iworks.at/.print_request.php', HTTP_METH_POST);
// if redirects is set to true, a single redirect is allowed;
// one can set any reasonable count of allowed redirects
$r->setOptions(
array( 'cookies' => array('MyCookie' => 'has a value'),
'redirect' => true,
)
);
// common form data
$r->setPostFields(
array( 'name' => 'Mike',
'mail' => 'mike#php.net',
)
);
// add the file to post (form name, file name, file type)
touch('profile.jpg');
$r->addPostFile('image', 'profile.jpg', 'image/jpeg');
try {
print $r->send()->getBody();
} catch (HttpException $e) {
print $e;
}
?>
I am working on a campaign monitor api which creates a custom list with custom fields.
When I try and add subscribers it used to work, now when I look at the list its not added them. Although its still returning a success code 201.
function addSubscriber($list_id, $emailAddress, $name, $title, $showName, $showDate, $showTime){
//create subscriber
$subscriber = array(
'EmailAddress' => $emailAddress,
'Name' => $name,
'CustomFields' => array(
array(
'Key' => "Title",
'Value' => $title
),
array(
'Key' => "ShowName",
'Value' => $showName
),
array(
'Key' => "ShowDate",
'Value' => $showDate
),
array(
'Key' => "ShowTime",
'Value' => $showTime
)
),
'Resubscribe' => true,
'RestartSubscriptionBasedAutoResponders' => true
);
//print_r($subscriber);
$subwrap = new CS_REST_Subscribers($list_id, $this->auth);
$result = $subwrap->add($subscriber);
//var_dump($result->response);
echo "Result of POST /api/v3.1/subscribers/{list id}.{format}\n<br />";
if($result->was_successful()) {
echo "Subscribed with code ".$result->http_status_code;
} else {
echo 'Failed with code '.$result->http_status_code."\n<br /><pre>";
var_dump($result->response);
echo '</pre>';
}
return $result->response;
}
This code is working for me.You need to add campaign monitor class files in you project folder and use valid list id and api key.You can find you api key from manage account link and list id from click on (change name/type) below list title. Please wait some time to see in you list.
require_once 'csrest_subscribers.php';
$name=$_POST['name'];
$email=$_POST['email'];
$wrap = new CS_REST_Subscribers('Your list ID', 'Your API Key');
$result = $wrap->add(array(
'EmailAddress' => $email,
'Name' => $name,
'CustomFields' => array(), // no custom fields, can remove this line completely
'Resubscribe' => true
));
echo "Result of POST /api/v3/subscribers/{list id}.{format}\n<br />";
if($result->was_successful()) {
echo "Subscribed with code ".$result->http_status_code;
} else {
echo 'Failed with code '.$result->http_status_code."\n<br /><pre>";
var_dump($result->response);
echo '</pre>';
}
I'm trying to post a message with picture (from a public URL, not from my albums) to group's wall:
$response = $facebook->api("/$group_id/feed", "POST", array(
'access_token=' => $access_token,
'message' => 'This is a test message',
'picture' => 'http://d24w6bsrhbeh9d.cloudfront.net/photo/agydwb6_460s.jpg',
)
);
Everything is fine, but I can't see a picture itself:
What am I doing wrong? Thanks in advance!
While posting a feed, you must specify a link along with a picture if want to add a picture to the feed; else its not a feed. Logical right?
Code:
'access_token=' => $access_token,
'message' => 'This is a test message',
'link' => '{link-to-share}',
'picture' => 'http://d24w6bsrhbeh9d.cloudfront.net/photo/agydwb6_460s.jpg',
If you dont want link, but just a picture, then you should simply upload the picture instead- by using /photos
Code:
$response = $facebook->api(
"/$group_id/photos",
"POST",
array (
'url' => '{image-url}',
)
);
Hello i'm trying to post on a user's wall a big foto article like this one
I have tried this code
$post = array(
'access_token' => $row['user_token'],
'message' => 'Hello world ',
'link' => $row['url'],
//'name' => "This is my title",
//'caption' => "My Caption",
//'description' => "Some Description...",
'picture' => $imgurl
//'image' => $row['img']
);
$res = $facebook->api("/" . $row['facebook_id']. "/feed", 'POST', $post);
and it produces a small picture like this one
And this code produces nothing
/* make the API call */
$response = $facebook->api(
"/me/objects/article",//or /$user_id/objects/article
"POST",
array (
'fb:app_id' => '302184056577324',//change with stuff
'og:type' => 'article',
'og:url' => 'Put your own URL to the object here',
'og:title' => 'Sample Article',
'og:image' => 'https://s-static.ak.fbcdn.net/images/devsite/attachment_blank.png',
)
);
/* handle the result */
Can someone provide working code or explain like you would to a 7 years old ?
If you want the image to be displayed at the larger size, the source image has to be at least 1200x630px as described at https://developers.facebook.com/docs/opengraph/howtos/maximizing-distribution-media-content#images and already pointed out by CBroe...
I am trying to post dynamic data to facebook wall.
I downloaded facebook api for php.
First of all, as for testing, I use below code.
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require '../src/facebook.php';
$config = array(
'appId' => 'myAPPID',
'secret' => 'MySecretCode'
);
//'access_token' => $access_token = $facebook->getAccessToken(),
$facebook = new Facebook($config);
$attachment = array(
'access_token' => 'my_tokenkey',
'message' => 'Test Message offline test message',
'name' => 'Name Test offline test...',
'description' => 'here goes description and links http:anotherfeed.com | http://facebook.com/anotherfeed',
'caption' => 'this is caption for action link',
'picture' => 'http://www.google.com/tv/images/slivetv.png',
'link' => 'www.yahoo.com',
'properties' => array(
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
),
);
try {
$facebook->api('/me/feed', 'POST', $attachment);
} catch(FacebookApiException $e) {
echo $e;
# handling facebook api exception if something went wrong
}
?>
As for my program requirement, I want my program to post data to facebook's wall whether facebook is online or offline.
But when I try this code, I get error.
OAuthException: An active access token must be used to query information about the current user.
Could anyone please give me suggestions?