Microsoft Teams + PHP cURL - Sending Message to specific channel - php

I have a problem with send message to Microsoft Teams. I followed the Microsoft Docs - https://learn.microsoft.com/en-us/graph/api/channel-post-messages?view=graph-rest-beta&tabs=http and something is not working.
I wrote following code:
$aToken = getAccessToken();
$dataArray = [
"body" => [
"content" => 'Hello World!'
]
];
$dataJSON = json_encode($dataArray, true);
$CURLHeaders = [
'Content-Type: application/json',
'Content-Length: ' . strlen($dataJSON),
'Authorization: ' . $aToken,
];
$ch = curl_init($postURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_KEEP_SENDING_ON_ERROR, true);
curl_setopt($ch, CURLOPT_HTTPGET, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJSON);
curl_setopt($ch, CURLOPT_HTTPHEADER, $CURLHeaders);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
and my $CURLHeader is:
(
[0] => Content-Type: application/json
[1] => Content-Length: 35
[2] => Authorization: Bearer eyJ0eXAiO...
)
and $dataJSON is:
{"body":{"content":"Hello World!"}}
After send it I get the message:
{
"error": {
"code": "UnknownError",
"message": "",
"innerError": {
"request-id": "88094f5d-1bcb-4875-9d49-880db19a146b",
"date": "2020-04-14T16:00:28"
}
}
}
Something is wrong, but I don't know what...

Related

Posting JSON Data With PHP cURL returns Invalid Content Type error

I'm working with OpenSRS EMAIL API. I'm trying to create a new user email for a domain. In their first example, they are using JSON format :
{
"credentials": {
"user": "domain_admin#example.com",
"password": "sw0rdf1sh"
},
"user": "bhayden#example.com",
"attributes": {
"name": "Bob Hayden",
"password": "changeit",
"delivery_forward": true,
"forward_recipients": [
"bob.hayden#example.com
}
}
I'm sending this with cURL
$json = array(
"user" => $emailName,
"attributes" => array(
"name" => "Janet User",
"password" => $email_password,
"delivery_forward" => false
)
);
//
//
$payload = json_encode( $json );
//
$data = [
'Content-Type:application/json',
'X-Username:' . $connection_details['reseller_username'],
'X-Signature:' . md5(md5($payload . $connection_details['api_key']) . $connection_details['api_key']),
];
//
$ch = curl_init($connection_details['api_host_port']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
//
$response = curl_exec($ch);
//
//header('Content-type: application/json');
//
echo '<pre>';
echo $response;
echo '</pre>';
This print following error
0.9 400 0 Invalid Content-Type XCP
What am I doing wrong?
I fixed this issue by reformarting the entire request. This worked for me
$data = array(
"user" => $emailName,
"attributes" => array(
"name" => "Janet User",
"password" => $email_password,
"delivery_forward" => false
)
);
$postdata = json_encode($data);
//
$ch = curl_init($connection_details_email['api_host_port']);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);

How send POST request with JSON in php?

I try to send request from post type to server but it not success, It return me BadRequest with status 400,
My code:
<?php
$products = array('1'=>array('amount'=>'1','product_id'=>'11250'));
$data = array('id' => '67', 'shipping' => '61', 'payment'=> '2','products'=> $products);
$cert = base64_encode('myapp#myapp.co.il:1234abcd');
$url = "https://myapp.co.il/api/aaa";
$ch = curl_init($url);
$payload = json_encode($data);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"authorization: Basic ".$cert,
"Content-Type: application/json",
"cache-control: no-cache"
));
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
?>
It return me so:
array (2) {["message"] => string (51) "Bad Request: Syntax error, distorted JSON" ["status"] => int (400)}
I see that you prepared variable $payload to send, but sent $postdata instead at curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata).

Firebase FCM error JSON_PARSING_ERROR: Unexpected token END OF FILE at position

I am trying to send notifications through firebase's fcm service using php. Here is what I got so far:
$ch = curl_init();
$payload = [
'to' => '/topics/'.ANDROID_TOPIC,
'notification' => [
'message' => 1
]
];
$headers = [
'Content-Type: application/json',
'Content-length: '.sizeof(json_encode($payload)),
'Authorization: key='.FIREBASE_KEY
];
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
$result = curl_exec($ch );
curl_close($ch);
return $result;
However, I am getting Unexpected token END OF FILE at position 2 response from firebase.
What do you think is the reason this is happening?
Remove Content-length line:
$header = array();
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: key=' . FIREBASE_KEY;
$payload = [
'to' => 'verybigpushtoken',
'notification' => [
'title' => "Portugal VS Germany",
'body' => "1 to 2"
]
];
$crl = curl_init();
curl_setopt($crl, CURLOPT_HTTPHEADER, $header);
curl_setopt($crl, CURLOPT_POST,true);
curl_setopt($crl, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($crl, CURLOPT_POSTFIELDS, json_encode( $payload ) );
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true );
// curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, false); should be off on production
// curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, false); shoule be off on production
$rest = curl_exec($crl);
if ($rest === false) {
return curl_error($crl)
}
curl_close($crl);
return $rest;

HTTP REQUEST in curl

how can I make an HTTP REQUEST in curl with json?
I need to put this in curl for php:
POST /api/ra/v1/ping HTTP/1.0
Host: app.kigo.net
Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
Content-Type: application/json
{
"PING" : "PONG"
}
Thanks in advance!
EDIT
This is what I've tried, but it shows "Invalid Content-Type header.":
<?php
$url = 'https://app.kigo.net/api/ra/v1/ping';
$headers = array( 'Authorization' => 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=', 'Content-Type' => 'application/json' );
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_POSTFIELDS, "PING=PONG");
$result = curl_exec($ch);
curl_close($ch);
print_r( $result );
?>
Your CURLOPT_HTTPHEADER array must be in the form array('Header1: value1', 'Header2: value2') and not array('Header1' => 'value1', 'Header2' => 'value2'), see examples in http://php.net/manual/en/function.curl-setopt.php
Also, if you post JSON data, you should probably json_encode() the data you are sending.
$headers = array(
'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=',
'Content-Type: application/json'
);
$postData = json_encode(array(
'PING' => 'PONG'
));
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://app.kigo.net/api/ra/v1/ping');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);

Issues with PAYPAL using cURL

I'm trying to create a payment (Paypal)
My data provided by paypal : https://developer.paypal.com/webapps/developer/docs/api/
$data = '{All the data}';
cURL..
$ch = curl_init();
$headers = array(
'Content-type: application/JSON',
'Authorization: Bearer A101.YqvcjeJ6va_06Zd3ZQSkn8uNeH0Il1KJKCy72pc0ReSZ_n-cA9oEbVSArBc78F-e.iVCsmw63xoQHrfe7W1v_MYGHhUS',
);
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response= curl_exec($ch);
var_dump($response);
$json = json_decode($result);
print_r($json);
curl_close($ch);
The response:
[name] => UNKNOWN_ERROR
[message] => An unknown error has occurred
[information_link] => https://developer.paypal.com/webapps/developer/docs/api/#UNKNOWN_ERROR
[debug_id] => 58039c1674622
So guys, what is wrong with my code?
Thanks for the help!

Categories