So I have the following CURL command in PHP, if I send it https://webhook.site I can extract all the required data in JSON format.
Instead of using webhook.site, I would like to create my own PHP webhook client.
The code below is the CURL command that works 100% when using webhook.site:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://webhook.site/832090f1-f54f-4847-8c0d-5ec9208541a1',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('SmsSid' => 'SMe8723661742d423fbf3fa9f7bbede050','SmsStatus' => 'sent','MessageStatus' => 'sent','ChannelToAddress' => '+1788123XXXX','To' => 'whatsapp:+15196978899','ChannelPrefix' => 'whatsapp','MessageSid' => 'SMe8723661742d423fbf3fa9f7bbede050','AccountSid' => 'AC306a09582e77715b0eb72df90de4c590','StructuredMessage' => 'false','From' => 'whatsapp:+154xxxxxx','MediaUrl0' => 'https://api.twilio.com/2010-04-01/Accounts/werwersdsdg72df90de4c590/Messages/wweugryuwyr7762b11ea/Media/wjeruwiy6243742
'),
CURLOPT_HTTPHEADER => array(
'user-agent: TwilioProxy/1.1',
'host: Postman'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
I then tried to use PHP to create a basic webhook just to pull the data:
<?php
if($json = json_decode(file_get_contents("php://input"), true)){
$data = $json;
$fp = file_put_contents( 'request.log', $data );
}
print_r($data);
?>
But I keep coming with a blank request.log file - what am I doing wrong??? Thanks in advance
Twilio developer evangelist here.
By default, curl will make a POST request with the Content-Type header application/x-www-form-urlencoded. This is actually the same content type that Twilio uses when it sends a webhook request.
Your PHP to receive the request is trying to json_decode the data, which won't work with form encoded data. Instead, you can access $_POST to get an associative array of the parameters sent to your script. You can then write them to a log file however you like.
Related
I'm trying to connect to the Azure platform to grab a response, mainly to get the token for use when accessing an office365 mailbox
The following is what i'm using, but I always get a NULL response
What other CURLOPT_POSTFIELDS need to be included, or what else needs to be changed.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://login.microsoftonline.com/".$tennantid."/oauth2/v2.0/authorize",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'client_id='.$appid.'&response_type=token&scope=https://graph.microsoft.com/User.Read&redirect_uri='.$uri,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
$response_decode = json_decode($response);
var_dump($response_decode);
curl_close($curl);
I currently get the token back ok when I use the following method
$params = array ('client_id' =>$appid,
'redirect_uri' =>$uri,
'response_type' =>'token',
'response_mode' =>'form_post',
'scope' =>'https://graph.microsoft.com/User.Read',
'state' =>$_SESSION['state']);
header ('Location: '.$login_url.'?'.http_build_query ($params));
Which works fine.
But I need to do CURL method as I need this running background cron job task
What do I seem to be missing?
Thanks in advance
A link to the API documentation may have helped. Without that I cannot help much. Just one obvious thing.
You are using Content-Type: application/x-www-form-urlencoded
You are not encoding the data. From MDN:
application/x-www-form-urlencoded: the keys and values are encoded in
key-value tuples separated by '&', with a '=' between the key and the
value. Non-alphanumeric characters in both keys and values are percent
encoded: this is the reason why this type is not suitable to use with
binary data (use multipart/form-data instead)
Source: MDN Post data
This would mean your request data should look like this:
client_id%3D1234%26response_type%3Dtoken%26scope%3Dhttps%3A%2F%2Fgraph.microsoft.com%2FUser.Read%26redirect_uri%3Dhttp%3Aexample.com
Try this:
$post = urlencode('client_id='.$appid.'&response_type=token&scope=https://graph.microsoft.com/User.Read&redirect_uri=http:example.com');
CURLOPT_POSTFIELDS => $ post,
I am trying to get the JSON data from a Website called
"https://thesimpsonsquoteapi.glitch.me/"
It has some sort of protection and take a while to open.
The Problem. After the site is loaded, you get access to some API calls like
"https://thesimpsonsquoteapi.glitch.me/quotes"
If I try it in Postman, I get back the right JSON response also if I open it up in the browser.
But if I try the to load the Data via curl in PHP i get the html code back from the first protection side. What can I do to get the JSON like in Postman.
$curl = curl_init();
$url = 'https://thesimpsonsquoteapi.glitch.me/quotes?count=1';
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
curl_close($curl);
thx for any help
I am trying to post a data to REST API using PHP/CURL and seems it's not working, as i get 301 Moved Permanently error,But it works fine with Postman. Here is the snippet of PHP CURL which i generated form Postman
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'api url',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('text' => 'test'),
CURLOPT_HTTPHEADER => array(
'api_key: key',
'Content-Type:application/json',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
And it always return 301 Moved permanently, Note if i change the API key i got Unauthorised error, which means it hits the server, but i am not sure what i am missing, I have tried with multiple headers combinations.
Any help on this regard would be highly appreciated.
Quoting the PHP manual :
CURLOPT_POSTFIELDS: This parameter can either be passed as a urlencoded
string like 'para1=val1¶2=val2&...' or as an array with the field
name as key and field data as value. If value is an array, the
Content-Type header will be set to multipart/form-data.
Since you use array you'll need to use multipart.
See this post : curl POST format for CURLOPT_POSTFIELDS
I have a Postman HTTP request using POST, a form data field of a file saved under the key plsm_xls_file[].
The file is in the local filesystem.
This request runs perfectly from POSTMAN but when I try to export it to PHP-Curl from the Code Snippets I get something like this:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://mydomain.nl/po_upload3.php?xlsimport=2',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('plsm_xls_file[]'=> new CURLFILE('/C:/Users/myuser/Documents/vita_debug/201216_FG_PC_68715.xlsx'),'template_id' => '170'),
CURLOPT_HTTPHEADER => array(
'cookie: PHPSESSID=509e15pepo3ok80nd74jhdis33;'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
It's not working. It's like the file isn't properly attached to the HTTP request.
EDIT: I finally understood that the problem was that POSTMAN has access to my filesystem but the remote server where I tried to run the exported snippet don't- A very silly mistake on my side.
I had this problem a while back, and while there was never a true resolution (even in PHP bug tracker), I was able to fix it by not including the file in the setopt_array command.
PHP 7.2 CURLFile Gives "Invalid Filename" Warning
In short, try taking your CURLOPT_POSTFIELDS option out of the curl_setopt_array call and add this:
curl_setopt($curl, CURLOPT_POSTFIELDS, array('plsm_xls_file[]'=> new CURLFILE('/C:/Users/myuser/Documents/vita_debug/201216_FG_PC_68715.xlsx'),'template_id' => '170'));
i am trying to upload images from my server to the img server where chevereto is installed but i cant get it to work
here is my code so far :
$directory = "/var/www";
$images = glob($directory . "/*.jpg");
foreach($images as $image)
{
echo $image;
$data = base64_encode(file_get_contents($image));
$run= shell_exec("curl --location --request POST \"http://ip/api/1/upload/?key=123456789&source=$data&format=json\"");
print_r($run);
}
getting error : 414 Request-URI Too Large
here is the api documentation : https://chevereto.com/docs/api-v1
You are using a POST request method, but you are sending the source parameter in URL.
The $data in your URL is base64 encoded, which results in a very large string. That's why you are getting the error 414 Request-URI Too Large.
Solution:
You should change the params to JSON body using the same POST method.
From the documentation of chevereto that you provided it is made clear.
API v1 calls can be done using the POST or GET request methods but since GET request are limited by the maximum allowed length of an URL you should prefer the POST request method.
So use POST method with JSON Body containing the parameters as key:value pair. That will solve the issue.
Sample PHP Code:
$requestData = [
'key' => 123456789,
'source' => 'base64EncodedStringHere',
'format' => 'json'
];
$jsonData = json_encode($jsonData);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ip/api/1/upload",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $jsonData,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;