i am trying to share a post on a page's wall using facebook api. everything works perfect . but the share button is not coming next to like and comment.
here is my code
$message_body = array(
'access_token' =>Yii::app()->session['page_access_token'],
'message' => $message,
'actions' => array(
array(
'name' => Yii::t('UserController', ' Get details '),
'link' => Yii::app()->createAbsoluteUrl ('user/adminmoreaboutprovider?&postId='.$fbPostId ),
),
),
);
$facebook->api("/".$userpage."/feed","post",$message_body);
Any idea how to bring share link there ?
$msg_body = array(
'message' => $message,
'actions' => array(
array(
'name' => 'Get details',
'link' => Yii::app()->createAbsoluteUrl('user/adminmoreaboutprovider?&postId='.$fbPostId ),
)
)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/6creeks/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, $msg_body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); //to suppress the curl output
$result= curl_exec($ch);
curl_close ($ch);
This gave me share lin but the get details links is missing on the Page wall. Is there any way to get both of this together?
With curl you can make a post (yes, along with the share button) using the following command.
curl -F 'access_token=XXXX' -F 'message=test' https://graph.facebook.com/[PAGE NAME]/feed
Hope this can be used as is in PHP.
Related
I want to post to a Synology Disksation and upload a file with PHP. The Synology's API gives me an interface that I want to call with PHP. See the documentation below.
https://global.download.synology.com/download/Document/DeveloperGuide/Synology_File_Station_API_Guide.pdf#%5B%7B%22num%22%3A111%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C69%2C711%2C0%5D
Here is my code, which I use for the post:
<?php
$params = array(
'path' => '/home/upload',
'create_parents' => 'true',
'overwrite' => 'true',
'api' => 'SYNO.FileStation.Upload',
'version' => 2,
'method' => 'upload'
'_sid' => [id of session after authenticate],
'file[]' => "#".path_to_file
);
$ch = curl_init();
$BODY = http_build_query($params);
curl_setopt($ch, CURLOPT_URL, 'http://ip_of_diskstation:5000/entry.cgi');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $BODY);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
var_dump($result);
?>
This call works, but the diskstation gives me the following JSON.
{"error":{"code":401},"success":false}
According to the documentation, there is a "Unknown error of file operation". Also, using "file_get_contents (path to file)" instead of "#".path_to_file gives me the same error from the diskstation.
How can I post a file and parameters to a synology diskstation?
Can you try to add _sid in your web api url like http://ip_of_diskstation:5000/entry.cgi?_sid=
I make it work using nodejs request library.
Below is the code:
let r = request.post( { url: `${web_url}/entry.cgi?_sid=${sid}` }, ( err, response, body ) => {
return callback( err, response );
});
let form = r.form();
form.append( 'api', 'SYNO.FileStation.Upload');
form.append( 'version', '2');
form.append( 'method', 'upload' );
form.append( 'overwrite', 'false' );
form.append( 'path', '<shared folder path>');
form.append( 'create_parents', 'true' );
form.append( '_sid', sid );
form.append( 'file', fs.createReadStream( file ), { 'filename' : <filename without the path>});
Regards,
Felix
You get :
"{"error":{"code":401},"success":false}"
Because the official document is wrong.
See Upload file through FileStation upload api for details.
From what I see the url that you used in CURLOPT_URL needs to be updated. Also the file that is in the $params needs to be updated.
Some side notes. Make sure your settings for the user's SID has permissions set properly. Also make sure you updated the $_FILES["uploadfile"] to include whatever name you used within the input element of the form (instead of uploadfile, which I used).
See this github link for where I found my reference. Postman helped me duplicate this in PHP.
$sid = '_sid=[YOURSID]';
$url = 'HOSTNAME/webapi/entry.cgi?' . $sid;
$SynologySharedPath = 'FILEPATH';
$params = array(
'api' => 'SYNO.FileStation.Upload',
'version' => '2',
'method' => 'upload',
'path' => $SynologySharedPath,
'create_parents' => 'true',
'overwrite' => 'true',
'file' => new \CurlFile($_FILES["uploadfile"]["tmp_name"], $_FILES["uploadfile"]["type"], $_FILES['uploadfile']['name'])
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_VERBOSE,true);
$result = curl_exec ($ch);
$curlresponse = json_decode($result, true);
I've been trying to create an account under a registrar account on Bitshares Test Net programatically using Graphene API and its blockchain. I read the API documentation and have concluded that the PHP code bellow is what I need to execute to be able to create a new account.
<?php
$url = 'https://testnet.bitshares.eu/';
$array = array(
'jsonrpc' => '2.0',
'method' => 'register_account',
'params' => array(
'name' => 'NEW_ACCOUNT_NAME',
'owner_key' => 'OWNER_KEY',
'active_key' => 'ACTIVE_KEY',
'registrar_account' => 'REGISTRAR_ACCOUNT',
'referrer_account' => 'REGISTRAR_ACCOUNT',
'referrer_percent' => 1,
'broadcast' => 1
),
'id' => 1
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'GraphenePHP/1.0');
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($array));
$response = curl_exec($ch);
curl_close($ch);
echo '<pre>';
print_r($response);
echo '</pre>';
The code above is not working. Also I am not getting any response. I have a feeling that I am missing something or the value of $url is incorrect.
Please help?
This Bitshares testnet link https://testnet.bitshares.eu/ sometimes does not work.
You can simply use real net or install testnet on your side for this.
I am using unification engine #unificationengine API to post message on facebook.
I followed all the steps and created connections to use connectors. All the curl requests are working fine till send message.
In every curl from create user, create connection, connection refresh I am getting
{'status':200,'info':'ok'}
And now I want to use the connector to post message on facebook.
Below is my Curl code:
$post_msg = json_encode(
array(
'message' =>
array(
'receivers' =>
array(
array(
'name' => 'Me',
'address' =>'https://graph.facebook.com/'.$request->profile_id.'/feed?access_token='.$request->access_token.'&message=Hello&method=post',
'Connector' => 'facebook'
),
),
'sender' =>
array('address' => 'sender address'),
'subject' => 'Hello',
'parts' =>
array(
array(
'id' => '1',
'contentType' => 'binary',
'data' => 'Hi welcome to UE',
'size' => 100,
'type' => 'body',
'sort' => 0
),
),
),
)
);
$ch = curl_init('https://apiv2.unificationengine.com/v2/message/send');
curl_setopt($ch, CURLOPT_USERPWD, "0a7f4444-ae4445-45444-449-d9b7daa63984:8755b446-6726-444-b34545d-713643437560");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_msg);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
var_dump($response);
return ['label' => $response];
and I am getting:
status: 403 and info: forbidden in response.
I have tried everything available in documentation and on stack overflow or any other website. But hard luck.
Please suggest why I am getting this error?
Refrence SO Questions:
SO question 1
SO question 2
Thanks.
Update
I added these three options in curl request:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, true);
and now I am getting 498, invalid access token error:
"{\"Status\":{\"facebook\":{\"status\":498,\"info\":\"Invalid Token:
\"}},\"URIs\":[] }
please use this as per php
public function facebookSharing($access_token) {
$app = new UEApp(env('UNIFICATION_APP_KEY'), env('UNIFICATION_APP_SECRATE'));
$user = new UEUser('unification_userkey', 'unification_usersecret');
$connection = $user->add_connection('FACEBOOK', "facebook", $access_token);
$options = array(
"receivers" => array(
array(
"name"=> "Me"
)
),
"message"=>array(
"subject"=>'testing',
"body"=> 'description',
"image"=> 'use any image url',
"link"=>array(
"uri"=> 'any web site url',
"description"=> "",
"title"=>"Title"
)
)
);
$uris = $connection->send_message($options);
}
The access token might have expired. Please reconnect the facebook connection again or refresh the connection.
The facebook access tokens have a lifetime of about two hours. For longer lived web apps, especially server side, need to generate long lived tokens. Long lived tokens generally lasts about 60 days.
UE has a capability to refresh facebook tokens. After adding connection using "apiv2.unificationengine.com/v2/connection/add"; api call, then you should call "apiv2.unificationengine.com/v2/connection/refresh"; api to make the short lived token to long lived.
I have created a php file that sends json data to an external url. Their API requires that I have a page on my website that receives the json responses for processing. The one that sends json data works well. I need help with the second php page that should process it
$Url="http://example.com/submit.php";
$date = date_create();
$UserID=7;
$Password='';//<-password written here
$Timestamp=date_timestamp_get($date);
$token=$UserID.$Password.$Timestamp;
$data_string = array();
$data_string = array(
"AuthDetails" => array(
array(
"UserID" => $UserID,
"Token" => md5($token),
"Timestamp"=>$Timestamp
)
),
"MessageType"=> array(
"3"
),
"BatchType"=>array(
"0"
),
"SourceAddr"=>array(
"Example"
),
"MessagePayload"=> array(
array(
"Text" => "Sample text message by Example :)"
)
),
"DestinationAddr" => array(
array(
'MSISDN'=>'254701000000',
'LinkID'=>''
)
),
"DeliveryRequest" => array(
array(
'EndPoint'=>'',//<-URL that receives the response
'Correlator'=>md5(uniqid())
)
)
);
$data_string=json_encode($data_string);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
)
);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$output = curl_exec($ch);
if(curl_errno($ch)){
echo 'Request Error:' . curl_error($ch);
}
curl_close($ch);
return $output;
Ok this is going to be a bit generic but it should put you on the right path.
So you have to write a script that will process the response from them i.e. your end-point for this circle of events.
So lets call it my-enpoint.php
So in the message you send to them you put the address of this new script into this parameter
"DeliveryRequest" => array(
array(
'EndPoint'=>'http://www.example.com/my-endpoint.php',
'Correlator'=>md5(uniqid())
Now your script my-endpoint.php I am assuming they have said how they will return their reply, probably as POST variable. You process their reply basically like you would a submitted form from your own site.
So for example if their reply is POSTed
<?php
// initial testing to see what comes back from them
// as this wont be associated with a browser
// dump their reply to a file so you can see whats there
file_put_contents('reply.txt', print_r($_POST, true), FILE_APPEND);
?>
With the info you have provided this is about a much as I can do.
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"