How to send notifications to an user with PHP? - php

I searched the Web everywhere to find an answer to my question, but i don't find any clear and simple answer... How to send a notification to an user with PHP ? We consider that the 'user' has installed the FB App.
I tried this code :
$post_data = "access_token=".$facebook->getAccessToken()."&template=My message&href=http://google.com";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://graph.facebook.com/".$user."/notifications/");
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($curl);
curl_close($curl);
print($page);
But I receive the following error message : {"error":{"message":"(#15) This method must be called with an app access_token.","type":"OAuthException","code":15}}
I don't using the right access_token ??
Thanks to help me or show me another code... :)

Try sending notification using this format with access token as querystring
$post_data = "access_token=".$facebook->getAccessToken()."&template=My message&href=http://google.com";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://graph.facebook.com/".$user."notifications?access_token= … &template= … &href= …");
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($curl);
curl_close($curl);
print($page);

Related

google chat bot webhook not working in php

Google chatbot has a webhook where users can automate chat there is a small python script to make it work but I want to do the same using php. I am using curl to hit the post method
$apiUrl = 'https://chat.googleapis.com/v1/spaces/AAAAbvoeZ0w/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&token=h2OIcCLB6_f_XxhPX6lugnGizE88ZBBpJURFHtPfh_0%253D';
$ch = curl_init($apiUrl);
//$kid_userid = "IXM_".session('user_data')['userID'];
$data = '{"text": "All Mock Test are Free "}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
after hitting this code I am getting errors.
{
error: {
code: 400,
message: "Invalid request token h2OIcCLB6_f_XxhPX6lugnGizE88ZBBpJURFHtPfh_0%3D",
status: "INVALID_ARGUMENT"
}
}
but same code when I am running on python it is working perfectly. Any Help will be very helpful. Thanks for the help in advance and I tried my best to explain .Sorry If I have made any mistake.
This code works for me. I have it from another SO answer, maybe the issue is the missing CURLOPT_POST...
$uri = "https://chat.googleapis.com/v1/spaces/AAAAgcWhzwk/messages?key=MY_KEYI&token=MY_TOKEN";
$msg = '*Testing Curl PHP message to Google Chat*\n\n Description';
$params = '{"text": "'.$msg.'"}';
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
The above code which I have shared is correct actually I just added the new bot URI and it worked. THanks, everyone for your help.

What's wrong with this PHP CURL script (integrating Foursquare API)?

I am trying to integrate Foursquare API to my website, here is the code:
$curl = curl_init('https://api.foursquare.com/v2/photos/add?v=20181008&oauth_token='.$token.'&photo='.$args['img'].'&venueId='.$title);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
// Decode the response
$data = json_decode($data);
// Verify if the post was published
if ( #$data->meta->code == 200) {
return true;
} else {
return false;
}
But when I run this script on my website and try to post image, it is showing
An error occurred while processing your request
Remove the lines
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
and try a simple
curl_setopt($curl, CURLOPT_POST, false);
You are sending the Request with GET Arguments
This could have multiple reasons.
I can't tell from the error message where exactly it triggers but when the curl fails, you should first investigate if there were any issues with the curl request itself.
Use curl_error for this: http://php.net/manual/de/function.curl-error.php
And are you using the API-Call the correct way?
In the docs POST https://api.foursquare.com/v2/photos/add
theres no $photo param at all.
Also when using POST you should add the parameters via
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
and not as GET-Parameters via URL.

PHP I want to send the post data by Codeigniter, How to make the post request?

I am developing the web server using other service by CodeIgniter.
So I am going to send the data to the service using api calling.
But I have a issue to make the request.
This is the data what I want to send.
$datastr = 'proc_name=customer_upd&params={
"proc_info":
{
"proc_division":"U"
},
"data":[{
"table_name":"Customer",
"rows":[ {
"itemId" : "12231551",
"lastName" : "ads",
"firstName" : "fds"
} ]
}]
}'
I made my code for this.
$curl = curl_init('https://webapi.ooo.com/access/');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded;charset=UTF-8'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $datastr);
curl_exec($curl);
But this is not work.
I am tested by postman, but it works well.
I think there is a issue to make the post request.
Please help me.
Thank you.
I think you are sending query string, instead, send only data in the key-value pair to the web service you are consuming. Check in which form web service is accepting the data. First, check web service from postman
Here is an example,
$data = array("key_1" =>value_1 , "key_2" =>"value_2" , "key_3" =>"value_3" , "key_4" =>"value_4" , "key_5" =>"value_5" );
function api($url,$api,$data)
{
$ch = curl_init();
$webservicelink = $url.$api;
curl_setopt($ch, CURLOPT_URL, $webservicelink);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'));
$result = curl_exec($ch);
return json_decode($result, true);
}

Displaying curl data, blank page

I'm trying to download json data using curl in PHP from companies house API.
Here is the example they provide that I'm trying to use:
https://developer.companieshouse.gov.uk/api/docs/company/company_number/readCompanyProfile.html#here
The example:
curl -uYOUR_APIKEY_FOLLOWED_BY_A_COLON: https://api.companieshouse.gov.uk/company/{company_number}
My code
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://api.companieshouse.gov.uk/company/01000000");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'curl -u8yUXrestOfmyApiKey:'
));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($curl);
if(false == $result){
echo curl_error($curl);
}
curl_close($curl);
print($result);
?>
Using header:
'-u my_api_key:8yUXGgcHznjaNWnKpuKIJ7yv7HDvU_slH273GuF1'
I get 400 - bad request and with header in my code above I get nothing no errors blank page no data.
PS. There is a company with number 01000000 I've checked
You shuld add CURLOPT_USERPWD
curl_setopt($curl, CURLOPT_USERPWD, "YOUR API KEY"); #Add your api key

Telegram Api - Creating an Authorization Key 404 error - PHP

I am trying to write a simple program in PHP to use telegram api, (not bot api, main messaging api). when i run my script, result is 404 error. Why?
<?php
$postfields = array();
$postfields['phone_number'] ='+1234567890';
$postfields['sms_type'] ='0';
$postfields['api_id'] ='12345';
$postfields['api_hash'] ='abcdefghijklmnopqrstuvwxyz';
$postfields['lang_code'] ='en';
$url = '149.154.167.40:443';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
echo $html=curl_exec($ch);
curl_close($ch);
?>
tanks a lot.

Categories