This is my code and I tried to "Curl" it (file_get_contents didn't work for some reason) but since i can't really get Curl to work, I came here to get some help.
I've been suffering with this for 10 hrs now and I still can't find out!!! please help!!
<?php
$app_id = "xxxxx";
$app_secret = "xxxxx";
$fanpage_id ='3xxxxx';
$post_login_url = "xxxxxxxxxteszt.php";
$photo_url = "xxxxxxxxxx20130412104817.jpg";
$photo_caption = "sasdasd";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if (!$code)
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream,manage_pages";
echo("<script>top.location.href='".$dialog_url."'</script>");
}
if(isset($_REQUEST['code'] ))
{
print('<script>alert("asd");</script>');
function curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $url );
}
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&client_secret=" . $app_secret
. "&redirect_uri=" . urlencode( $post_login_url)
. "&code=" . $code;
print($code);
$response = curl($token_url);
print($response);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// POST to Graph API endpoint to upload photos
$graph_url= "https://graph.facebook.com/".$fanpage_id."/photos?"
. "url=" . urlencode($photo_url)
. "&message=" . urlencode($photo_caption)
. "&method=POST"
. "&access_token=" .$access_token;
echo '<html><body>';
echo curl($graph_url);
echo '</body></html>';
}
?>
You can't just pass the url to the CURLOPT_POSTFIELDS option. The base url should be set in the CURLOPT_URL option, and the parameters in CURLOPT_POSTFIELDS (using PHP's http_build_query function to generate the encoded query string);
So your curl function could look something like this:
function curl($url,$parms)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parms, null, '&'));
}
And then you would call it like this:
$token_url="https://graph.facebook.com/oauth/access_token";
$token_parms = array(
"client_id" => $app_id,
"client_secret" => $app_secret,
"redirect_uri" => $post_login_url,
"code" => $code
);
$response = curl($token_url, $token_parms);
Related
I send messages with facebook application, the messages are texts
I want to send link in the message.
This is my code :
$message_to_reply = 'hello';
$ch = curl_init($url);
$jsonData = '{
"recipient":{
"id":"' . $sender . '"
},
"message":{
"text":"' . $message_to_reply . '"
}
}';
$jsonDataEncoded = $jsonData;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
But it doesnt work.
I want to get my form data back from kobo api (https://kc.kobotoolbox.org/api/v1/). I registered client application from kobo. To authorize client application, i run below url but it results in
http://localhost/kobo/o/authorize?client_id=MY_CLIENT_iD&response_type=code&state=xyz
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.27 (Win32) OpenSSL/1.0.2l PHP/7.1.9
Similarly request for access token also result in same error
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost/o/token/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=authorization_code&\ncode=PSwrMilnJESZVFfFsyEmEukNv0sGZ8&\nclient_id=MY_CLIENT_ID&\nredirect_uri=http://localhost:30000");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "USERNAME" . ":" . "PASSWORD");
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
echo $result;
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.27 (Win32) OpenSSL/1.0.2l PHP/7.1.9
I am using xampp and curl is enabled.
You have to use https://kc.kobotoolbox.org/api/v1/ not https://kf.kobotoolbox.org/api/v1/
checkout docs here http://help.kobotoolbox.org/managing-your-project-s-data/rest-services
MY WORKING CODE:
$url = 'https://kc.kobotoolbox.org/api/v1/stats';
$data = array (
// 'q' => 'nokia'
);
$params = '';
foreach($data as $key=>$value)
$params .= $key.'='.$value.'&';
$params = trim($params, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url.'?'.$params ); //Url together with parameters
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 7); //Timeout after 7 seconds
//curl_setopt($ch, CURLOPT_HEADER , false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Token xxxxxxxx';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//curl_setopt($ch, CURLOPT_USERPWD, "USERNAME" . ":" . "PASSWORD");
$result = curl_exec($ch);
if(curl_errno($ch)) //catch if curl error exists and show it
echo 'Curl error: ' . curl_error($ch);
echo $result;
I am trying to do a put request to a RESTapi using cURL. I was able to do post request. But PUT is not working
I used following code
public function callTeamworkPutApi($secretApiKey, $apiCallString,$param)
{
//cURL
$password = "xxx";
$channel = curl_init();
//options
curl_setopt($channel, CURLOPT_URL, "http://projects.abounde.com/".$apiCallString); // projects.json?status=LATE gets all late projects
$headers = array();
$headers[] = "Authorization: Basic " . base64_encode($secretApiKey . ":" . $password);
$headers[] = "Content-Type: application/json";
curl_setopt($channel, CURLOPT_HTTPHEADER, $headers);
curl_setopt($channel, CURLOPT_PUT, true);
//curl_setopt($channel, CURLOPT_POSTFIELDS, $comment);
curl_setopt($channel, CURLOPT_PUTFIELDS, $param);
// curl_setopt($channel, CURLOPT_RETURNTRANSFER, true);
$msg = curl_exec($channel);
$tester =var_dump(curl_getinfo($channel));
curl_close($channel);
//var_dump($msg);
return response()->json(['res'=>$tester]);
}
Error:
Any Idea?
Just been doing that myself today. here is example code.
$data = array("a" => $a);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
if (!$response)
{
return false;
}
src: http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl
I'm trying to make a request to Paypal to get a new access token, but when trying a simple curl call, it's returning with this error -
"Invalid_request - Invalid caller, requires client_id & secret"
Not quite sure what's wrong with this. The client id and secret are correct and are included.
Here is what I've tried -
$clientId = "XXX";
$secret = "XXX";
$url = "https://api.sandbox.paypal.com/v1/oauth2/token";
$method = 'POST';
$postvals = sprintf("client_id=%s&client_secret=%s&grant_type=%s&refresh_token=%s",
$clientId,
$secret,
'refresh_token',
$refreshToken);
$ch = curl_init($url);
if ($method == 'POST'){
$options = array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postvals,
);
curl_setopt_array($ch, $options);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/json",
"Accept-Language: en_US",
"Content-Type: application/x-www-form-urlencoded",
'Authorization: Bearer '.$old_accessToken,
"Host: www.paypal.com",
"Connection: close"
)
);
$result = curl_exec($ch);
// Get Request and Response Headers
$requestHeaders = curl_getinfo($ch, CURLINFO_HEADER_OUT);
$responseHeaderSize = strlen($result) - curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD);
$responseHeaders = substr($result, 0, $responseHeaderSize);
//only use body and not header, otherwise return is NULL
$result = substr($result, $responseHeaderSize);
if(!$result || empty($result)){
echo 'Curl error or response is empty: ' . curl_error($ch);
curl_close($ch);
} else {
curl_close($ch);
$response =json_decode($result);
var_dump($response);
}
This solution worked instead -
$clientId = "XXXX";
$clientSecret = "XXXX";
$url = "https://api.sandbox.paypal.com/v1/oauth2/token";
$method = 'POST';
$postdata = sprintf("client_id=%s&client_secret=%s&grant_type=%s&refresh_token=%s",
$clientId,
$secret,
'refresh_token',
$refreshToken);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $clientId . ":" . $clientSecret);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
# curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
$response = curl_exec( $curl );
if (empty($response)) {
// some kind of an error happened
die(curl_error($curl));
curl_close($curl); // close cURL handler
} else {
$info = curl_getinfo($curl);
echo "Time took: " . $info['total_time']*1000 . "ms\n";
curl_close($curl); // close cURL handler
if($info['http_code'] != 200 && $info['http_code'] != 201 ) {
echo "Received error: " . $info['http_code']. "\n";
echo "Raw response:".$response."\n";
die();
}
}
// Convert the result from JSON format to a PHP array
$jsonResponse = json_decode( $response );
I have a Facebook App that POST a picture to a specific album created also by my app. If I run the app the second time it will create another album and POST the new picture there. I would like if the user POST a new one, the picture to be placed in the same album and if is not there to be created. Here is my code so far:
$graph_url = "https://graph.facebook.com/me/albums?" . "access_token=". $access_token;
$album_name = 'MyApp Album';
$album_description = 'Album description';
$postdata = http_build_query(
array('name'=> $album_name,
'message'=> $album_description
));
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=> 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
));
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false, $context));
$album_id = $result->id;
$file = $_POST['photo'];
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = "https://graph.facebook.com/".$album_id."/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_POSTFIELDS, $args);
$data = curl_exec($ch);
print_r(json_decode($data,true));
I don't wanna use FQL queries if isn't absolutely neccesary. If I can search probably for the album name and then return the ID it would be great.