I've set up a script to post news to my facebook page via PHP
It worked for the last 2 years
Now, without notice it stopped working.
I correctly get the access_token but the second part returns this error
{"error":{"message":"An unknown error has occurred.","type":"OAuthException","code":1}}
Here is the code
$url = "https://graph.facebook.com/oauth/access_token";
$postString = "client_id=KEY&client_secret=SECRET&type=client_cred";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
$access_token = str_replace( "access_token=", "", curl_exec($curl) );
$titolo = 'Test';
$link_pulito = 'test.html';
$testo_fb = 'Test';
$attachment = array(
'access_token' => $access_token,
'message' => 'MESSAGE',
'name' => 'test',
'link' => 'http://www.test.com/workshop/',
'description' => 'test test test',
'picture'=>'http://www.test.com/77818763a19937bdd82b25f26cef2522.jpg'
);
// set the target url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/MYPAGE/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, false); //to suppress the curl output
$result= curl_exec($ch);
curl_close ($ch);
Facebook has changed the way of leasing tokens.
Please use the facebook PHP SDK from https://github.com/facebook/facebook-php-sdk
Related
I want to consume a web service by post method, and I have these fields:
and I am sending the information in this way;
$post_fields = array('nombre' => 'uno', 'apellido' => 'dos');
PHP code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query( $post_fields ) );
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
But it does not bring me information. Does anyone know how to receive the data or what my error is?
Based on the documentation of Twilio and Curl I have a php curl routine:
function twilio($mobile,$msg,$twoCode){
$url = 'https://api.twilio.com/2010-04-01/Accounts/'.TWILIO_ACCOUNT_SID.'/Calls.json';
$CallURL = 'http://Myweb.com/code/say/'.$twoCode;
$auth = TWILIO_ACCOUNT_SID.":".TWILIO_AUTH_TOKEN;
$fields = array(
'To' => $mobile ,
'From' => '+16262471234' , // My Number
'Url' => urlencode( $CallURL ) ,
'Method'=>'GET' ,
'FallbackMethod'=>'GET',
'StatusCallbackMethod'=>'GET',
'Record'=>'false'
);
$post = http_build_query($fields);
$curl = curl_init($url);
// Set some options - we are passing in a useragent too here
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT , 'Mozilla 5.0');
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: 7' ));
curl_setopt($curl, CURLOPT_USERPWD, $auth);
curl_setopt($curl, CURLOPT_VERBOSE , true);
$resp = curl_exec($curl);
curl_close($curl);
}
It gives me an error:
{"code": 21213, "message": "No 'From' number is specified", "more_info": "https://www.twilio.com/docs/errors/21213", "status": 400}
I tried all the options, can anyone help?
I edited and added a "+" for the "From" number. But still the error remains the same.
Thanks in advance!
Twilio Developer Evangelist here.
Your code is really close. Just two small changes should resolve your issue. First you need to remove this line:
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: 7' ));
This was truncating your POST data which resulted in the "From" information not being sent.
Second, you don't need to urlencode the $CallURL because curl handles this for us.
Once you make both of these changes your code should look like this and run without an error:
function twilio($mobile,$msg,$twoCode){
$url = 'https://api.twilio.com/2010-04-01/Accounts/'. TWILIO_ACCOUNT_SID.'/Calls.json';
$CallURL = 'http://Myweb.com/code/say/'.$twoCode;
$auth = TWILIO_ACCOUNT_SID .":". TWILIO_AUTH_TOKEN;
$fields = array(
'To' => $mobile ,
'From' => '+16262471234' , // My Number
'Url' => $CallURL,
'Method'=>'GET' ,
'FallbackMethod'=>'GET',
'StatusCallbackMethod'=>'GET',
'Record'=>'false'
);
$post = http_build_query($fields);
$curl = curl_init($url);
// Set some options - we are passing in a useragent too here
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT , 'Mozilla 5.0');
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
curl_setopt($curl, CURLOPT_USERPWD, $auth);
curl_setopt($curl, CURLOPT_VERBOSE , true);
$resp = curl_exec($curl);
curl_close($curl);
}
Let me know if this gets your issues resolved.
Currently, to POST image from local folder I use curl and PHP, here is my code:
$url = 'http://myprestashop.com/api/images/products/1';
$image_path = 'C:\\my_image.png';
$key = 'My web service key';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, $key.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '#'.$image_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
How can I POST image from URL (http://imageserver.com/image.jpeg) instead of local folder (C:\my_image.png)? Is it possible?
Just change your image path and try this code.
$url = 'http://myprestashop.com/api/images/products/1';
$data = array('key' => 'My web service key', 'image_path' => '#/home/user/test.png');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
I have this code for logging into Google using Simple DOM Parser with curl. I've tried to post status on google plus using following code but unable to post on google plus
Any idea on how to solve this?
Here's my code for reference:
$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => "youremail#gmail.com",
"Passwd" => "yourpassword",
"service" => "writely",
"source" => "your application name"
);
// Initialize the curl object
$curl = curl_init($clientlogin_url);
// Set some options (some for SHTTP)
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Execute
$response = curl_exec($curl);
// Get the Auth string and save it
preg_match("/Auth=([a-z0-9_-]+)/i", $response, $matches);
$auth = $matches[1];
$params['newcontent'] = "Post on Google Plus Test By Me";
$headers = array(
"Authorization: GoogleLogin auth=" . $auth,
"GData-Version: 3.0",
);
// Make the request
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($curl);
curl_close($curl);
Thanks For your help and co-operation
There is not a Google+ API to post statuses (unless you are a Google Apps user) and ClientLogin has been deprecated and will stop working soon. You're best bet is to look into using something like the share plugin.
In order not show the moved you have to set the agent header like firefox or something else
example
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl,"USER_AGENT","firefox my browser");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Added here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Added here
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Since you got 301 redirect
$response = curl_exec($curl);
var_dump($response);// Added here
curl_close($curl);`enter code here`
You need to add these two cURL params here too.
curl_setopt($curl, CURLOPT_URL, 'https://www.plus.google.com/');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //Added here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //Added here
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // Since you got 301 redirect
Also, you are not outputting any response on your code
$response = curl_exec($curl);
var_dump($response);// Added here
curl_close($curl);
I am trying to sequentially execute multiple curl requests, but I am not really having any luck, here is my code thus far,
//upload photo
$file= 'photo.jpg';
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = 'https://graph.facebook.com/me/photos?access_token='.$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_POSTFIELDS, $args);
$data = curl_exec($ch);
$args_two = array(
'message' => 'This is a test post',
);
$ch_two = curl_init();
$url_two = 'https://graph.facebook.com/me/feed?access_token='.$token;
curl_setopt($ch_two, CURLOPT_URL, $url_two);
curl_setopt($ch_two, CURLOPT_HEADER, false);
curl_setopt($ch_two, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_two, CURLOPT_POST, true);
curl_setopt($ch_two, CURLOPT_POSTFIELDS, $args_two);
$data_two = curl_exec($ch_two);
This file gets called and then the two requests should be executed one after the other, but right now it's just not working.
Thanx in advance!
Try to use curl_close
Try to use the same curl resource for two requests
Check if is it true, that none of requests is send to servers