Php curl post request to pinterest not working - php

Changing the code to an access token gives this error:
Error: { "status": 403, "message": "Forbidden" }
This is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,"grant_type=authorization_code&client_id=4853355452345362719&client_secret=deb78310995ec1cf00918a5e688e2148e6043bd640ab16f0f7ecd7543b4ac764&code=".$code);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result =curl_exec ($ch);
curl_close($ch);
print_r($result);

First, you need to generate an access token. This can be done CURLing the following URL:
"https://api.pinterest.com/oauth?response_type=code&redirect_uri={$callBackUrl}&client_id={$clientId}&scope=read_public,write_public"
This will return an authorization code, you can then use this to generate your access token using this URL: https://api.pinterest.com/v1/oauth/token
with these parameters.
$url = "https://api.pinterest.com/v1/oauth/token";
$body = "grant_type=authorization_code&client_id={$clientId}&client_secret={$clientSecret}&code={$code}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$content = curl_exec($ch);
curl_close($ch);
$data = json_decode($content, true);

403 Forbidden usually suggests a permissions error. I'd bet the remote API does not recognize the code you're sending

Related

Why telegram bot sends the file twice?

I am sending a file via telegram bot api, I am sure that my code works once, but two files always come! I cannot understand what is the reason, here is my code:
$url = "https://api.telegram.org/bot<Token>/sendDocument?chat_id=<chatId>&caption=Yourfile";
$document = new CURLFile('test.txt');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ["document" => $document]);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type:multipart/form-data"]);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$out = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
exit('File send');

Reddit POST Request - Error 403 (php/curl)

I'm trying to connect with the Reddit API and programatically submit posts. I have managed to return an access token but I get 403 error when trying to do the actual submission. Any advice?
Get Access Token: (works, returns token)
$ch = curl_init('https://www.reddit.com/api/v1/access_token?grant_type=client_credentials&username=USERNAME&password=PASSWORD');
curl_setopt($ch, CURLOPT_USERPWD, 'CLIENTID:CLIENTSECRET');
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
curl_close($ch);
$token = json_decode($return, true);
Submit Link: (error 403)
$ch = curl_init('https://www.reddit.com/api/v1/submit?kind=link&sr=news&title=TITLE&r=news&url='.urlencode('http://example.com'));
curl_setopt($ch, CURLOPT_USERPWD, 'CLIENTID:CLIENTSECRET');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: bearer ".$token['access_token']));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
curl_close($ch);
return json_decode($return, true);

{"int_err_code":"Authentication Failed","msg":"Wrong grant type"} trying to get access_token and organizer_key in citrix api

I am getting {"int_err_code":"Authentication Failed","msg":"Wrong grant type"} trying to get access_token and organizer_key. I use GoToWebinar API.
Here is my curl:
$http_headers=array(
'Accept:application/json',
'Content-Type:application/x-www-form-urlencoded'
);
$URL = 'https://api.citrixonline.com/oauth/access_token?grant_type=authorization_code'
.'&code='.$response_key
.'&client_id='.$consumer_key;
// send curl post request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
$response = curl_exec($ch);
curl_close($ch);

How to post to a facebook users wall using the php api with the access token and curl?

Here is what I am currently trying:
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me/feed');
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=$facebook_access_token&message=testing api.");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
}
where $facebook_access_token is a variable containing the access token.
Am I doing something wrong? I have to use the php api and I do not explicitly know the users facebook page url, only the access token.
Thanks.
Add one more option to cURL:
CURLOPT_SSL_VERIFYPEER : false
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me/feed');
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=$facebook_access_token&message=testing api.");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
}

How to use CURL instead of file_get_contents?

I use file_get_contents function to get and show external links on my specific page.
In my local file everything is okay, but my server doesn't support the file_get_contents function, so I tried to use cURL with the below code:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo file_get_contents_curl('http://google.com');
But it returns a blank page. What is wrong?
try this:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
This should work
function curl_load($url){
curl_setopt($ch=curl_init(), CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
$url = "http://www.google.com";
echo curl_load($url);
I encountered such a problem accessing Google Drive content via the direct link.
After calling file_get_contents returned 302 Moved temporarily
//Any google url. This example is fake for Google Drive direct link.
$url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";
$html = file_get_contents($url);
echo $html; //print none because error 302.
With the code below it worked again:
//Any google url. This example is fake for Google Drive direct link.
$url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$html = curl_exec($ch);
curl_close($ch);
echo $html;
I tested it today, 03/19/2018
//You can try this . It should work fine.
function curl_tt($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo curl_tt("https://google.com");

Categories