I want to auto share links in my facebook wall using this script :
$attachment = array(
'email' => 'mail',
'password' => 'password',
'access_token' => 'my token',
'message' => "my message",
'name' => 'name',
'link' => 'my_url',
'description' => "my description"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me/links');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101');
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$json = json_decode($result);
$post_id = $json->{'id'};
curl_close($ch);
at the began it worked for me but after trying it for 7 times or more i got this error :
{ "error": { "message": "(#100) The parameter url is required", "type": "OAuthException", "code": 100 } }
how can I fix it
links
Create
You can post a link on the user's behalf by issuing an HTTP POST request to PROFILE_ID/feed with the publish_stream permissions.
The other fields are taken from the metadata of the page URL given in the 'link' param.
If the create is successful, you get the following return.
Refer to: https://developers.facebook.com/docs/reference/api/user/#links
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me/feed');
Related
Im trying to upload files via IPBoards REST Api but dont really know how to format the files.
This is how it looks right now but only gets error:
{
"errorCode": "1L296\/B",
"errorMessage": "NO_FILES"
}
Code:
$post = array(
'category' => 1,
'author' => 1,
'title' => 'Test title',
'description' => 'test description',
'files' => "{'test.txt':'".file_get_contents('/home/test/test.txt')."'}",
);
$target_url = 'https://example.com/api/downloads/files';
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 86400);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD,$apikey.":");
$result = curl_exec ($ch);
curl_close ($ch);
Here is the api documentation: API doc
Try using the http_build_query method and change the $post array slightly:
'files' => array($filename => $contents),
And change to:
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
I am trying to create ad account in Facebook business manager via Facebook marketing and graph API using following code.
$attachment = array('access_token' => $this->accessToken,
'name' => $associative_arr['name'],
'currency' => $associative_arr['currency'],
'timezone_id' => $associative_arr['timezone_id'],
'end_advertiser' => $this->mybusinessId,
'media_agency' => 'NONE',
'partner' => 'NONE',
'access_type' => 'OWNER',
'permitted_roles' => 'ADMIN'
//'user_role' => '1001'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$this->apiVersion.'/'.$this->mybusinessId.'/adaccount');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
$dcde = json_decode($result);
curl_close ($ch);
It successfully creates ad account but does not add me as a user in people with administrative access.
Can anyone give me suggestion what can be the reason?
Once the ad account is created, you'll need to make another call which will add the user with the required permissions.
$attachment = array(
'access_token' => $this->accessToken,
'business' => '<business_id>',
'user' => '<user_id>',
'role' => 'ADMIN'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$this->apiVersion.'/act_<AD_ACCOUNT_ID>/userpermissions');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
$dcde = json_decode($result);
curl_close ($ch);
I am trying to execute the background REST API Call with Curl
library in php. I guess it is not working.
can you suggest me ?
$cum_url = http://localhost/test/list;
$post = [ 'id' => $object->id ];
$ch = curl_init($cum_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
curl_exec($ch);
curl_close($ch);
UPDATE: Cull error says "Timeout was reached".
Thanks,
Raja K
You need to transfer your post data from array to http parameter string, ex:
$cum_url = "http://localhost/test/list";
$post = [ 'id' => $object->id ];
$postdata = http_build_query($post);
$options = array (CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_AUTOREFERER => true,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1",
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false);
$ch = curl_init($cum_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt_array ( $ch, $options );
$res = null;
if(!curl_errno($ch)) {
$res = curl_exec($ch);
}
curl_close($ch);
Of course, some option is optional depends on you, ex CURLOPT_USERAGENT. Just show you an example.
In my application, I am posting images on walls, and it works fine, but now my requirement is to print my app's URL with a message and have it so that when a user clicks on the link, he is redirected to my app.
You can use curl method, Am using this and it works fine
$attachment = array(
'access_token' => "accesstoken",
'caption' => "Hey -- this is caption",
'description' => "Your Desc",
'link' => "http://www.example.com/",
'picture'=> "path/image.png"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/XXXXXX/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
I use this to post on users wall, but it shows a blank page, and the post doesn't appear on my wall :
$url = "https://graph.facebook.com/user_id/feed";
$ch = curl_init();
$attachment = array( 'access_token' => access_token_here,
'name' => "Rave Kenya",
'link' => "www.youtube.com",
'description' => 'Testing a new facebook app',
'message' => 'Tested',
);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
$result =curl_exec($ch);
Why reinvent the wheel and not use facebook php library?
And a step-by-step instructions are available at "5 Steps to publish on a facebook wall using php"