How to call a REST API in laravel 5.4 - php

I have developed a laravel project in which I want to call a REST API to pass a form inputs to it by the use of CURL. After submitting a form by user, form data are passed to a controller, then API is called but I receives false or null as response.
The data must be sent to API as below, as you see the data must be a json in an array(image is not a file, it is a path):
[{
'title' : 'title',
'body' : 'description',
'source' : 'http://www.google.com',
'date' : 1503845107465,
'active' : true,
'image' : '758.jpg',
'category' : [
1
]
}];
here is a part of my controller, $data is build out of form inputs.
$data = [
'title' => 'title',
'body' => 'description',
'source' => 'http://www.google.com',
'date' => 1503845107465,
'active' => true,
'image' => '758.jpg',
'category' => [
1
]
];
$curl=curl_init('http://x.x.x.x/rest/test');
$send = json_encode([$data]);
//return $send;
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $send);
try{
$res = curl_exec($curl);
}catch (Exception $e){
return $e->getMessage();
}
dd($res);
$res is false.
It must be mentioned that the code in controller works on localhost but it does not work on linux server.
I also used ixudra/curl but it does not solve my problem and it returns null.
I got this error Failed to connect to x.x.x.x Permission denied. I can ping this url and I can send data with curl to this IP in a php file. but in laravel I get this error

Related

Migration of Slack app which uses event api (HTTP end-point) to Slack Socket Mode

I have implemented a Slack app using PHP(HTTP-endpoint). Under slack slash commands, I have inserted the request URL (HTTP end-point which points to the code I have written in PHP to open a Slack Modal ) that opens a dialogue box (Slack Modal). And upon submitting the form it will go to GitHub, that I have inserted the URL of my code written in PHP under request URL in interactivity and shortcuts(Payload).
But due to security measures, I want to migrate the app to socket mode. Can someone please let me know how to implement the socket mode for PHP in my local VM?
This is code to open a dialog box in slack
<?php
$slack_token = 'xoxb-******************';
// Dialog Form:
$dialog = [
'callback_id' => 'git_issue',
'title' => 'Add issue',
'submit_label' => 'Create',
'elements' => [
[
'type' => 'text',
'label' => 'Name',
'name' => 'name'
],
[
'type' => 'text',
'label' => 'E-mail',
'name' => 'email'
],
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/dialog.open');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// set the POST query parameters
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));
// execute curl request
$response = curl_exec($ch);
How to migrate the application to socket mode?
Thanks in Advance
I use this own-written code. May be it is not perfect, but it works:
<?php
require 'vendor/autoload.php';
// Slack config:
$slack_token = 'xapp-*';
$bot_token = 'xoxb-*';
$url = false;
$dialog = [
'callback_id' => 'git_issue',
'title' => 'Add new GitHub Issue',
'submit_label' => 'Create',
'elements' => [
[
'type' => 'text',
'label' => 'Developer\'s Name',
'name' => 'name'
],
]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/apps.connections.open');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded',
'Authorization: Bearer '.$slack_token
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// set the POST query parameters
curl_setopt($ch, CURLOPT_POST, true);
// execute curl request
$response = curl_exec($ch);
// close
curl_close($ch);
if ($response) {
$resp = json_decode($response);
if (isset($resp->url)) {
$url = $resp->url;
}
}
if ($url) {
echo 'Slack returned the websocket URL'.PHP_EOL;
$client = new WebSocket\Client($url, ['timeout' => 3600]);
while (true) {
try {
$message = $client->receive();
echo $message.PHP_EOL.PHP_EOL;
if ($message) {
$data = json_decode($message);
if (isset($data->payload)) {
if (isset($data->payload->command) && $data->payload->command == '/git') {
// Open Dialog:
// define POST query parameters
$query = [
'token' => $bot_token,
'dialog' => json_encode($dialog),
'trigger_id' => $data->payload->trigger_id,
];
// define the curl request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/dialog.open');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// set the POST query parameters
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));
// execute curl request
$response = curl_exec($ch);
// close
curl_close($ch);
$query = [
'envelope_id' => $data->envelope_id
];
$client->text(json_encode($query));
// -----------
}
if (isset($data->payload->type) && $data->payload->type == 'dialog_submission') {
// collect submitted data:
$name = $data->payload->submission->name;
$query = [
'envelope_id' => $data->envelope_id,
];
$client->text(json_encode($query));
}
}
}
} catch (\WebSocket\ConnectionException $e) {
// Possibly log errors
var_dump($e);
}
}
$client->close();
}
Configure your Slack app and create tokens:
go to https://api.slack.com/apps page and select existed one or
create a new app
go to "basic information" page, find "App-Level
Tokens" section. Create a new token. I use these scopes
"connections:write, authorizations:read" for it
go to "OAuth & Permissions" page and generate "Bot User OAuth Token"
go to "slash commands" page and create a command. In my code example we have it with the name "/git"
go to "Socket Mode" page and enable the mode with a slide button
For websockets connection via PHP I use this Textalk/websocket-php realization from GitHub.

Slack sends invalid, mal-formed JSON data after Dialog interaction

I am working on a slash command that'll invoke a dialog.
$dialog = [
'callback_id' => 'ryde-46e2b0',
'title' => 'Request a Ride',
'submit_label' => 'Request',
'elements' => [
[
'type' => 'text',
'label' => 'Pickup Location',
'name' => 'loc_origin'
],
[
'type' => 'text',
'label' => 'Dropoff Location',
'name' => 'loc_destination'
]
]
];
// get trigger ID from incoming slash request
$trigger = filter_input(INPUT_POST, "trigger_id");
// define POST query parameters
$query = [
'token' => 'XXXXXXXXX MY TOKEN XXXXXXXXX',
'dialog' => json_encode($dialog),
'trigger_id' => $trigger
];
// define the curl request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/dialog.open');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// set the POST query parameters
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));
// execute curl request
$response = curl_exec($ch);
// close
curl_close($ch);
var_export($response);
When I issue the slash command, my test dialog opens successfully
I then fill two test values test1 and test2 in the fields and submit the request. My endpoint is being hit with the dialog data payload correctly, but the data sent is not valid JSON:
The value of $_POST is: (I've masked all identifying tokens/IDs with xxx)
{"payload":"{\\\"type\\\":\\\"dialog_submission\\\",\\\"token\\\":\\\"XXX\\\",\\\"action_ts\\\":\\\"1536603864.688426\\\",\\\"team\\\":{\\\"id\\\":\\\"xxx\\\",\\\"domain\\\":\\\"ourdomain\\\"},\\\"user\\\":{\\\"id\\\":\\\"xxx\\\",\\\"name\\\":\\\"my_name\\\"},\\\"channel\\\":{\\\"id\\\":\\\"xxx\\\",\\\"name\\\":\\\"directmessage\\\"},\\\"submission\\\":{\\\"loc_origin\\\":\\\"test1\\\",\\\"loc_destination\\\":\\\"test2\\\"},\\\"callback_id\\\":\\\"ryde-46e2b0\\\",\\\"response_url\\\":\\\"https:\\\\/\\\\/hooks.slack.com\\\\/app\\\\/XXX\\\\/XXX\\\\/XXX\\\",\\\"state\\\":\\\"\\\"}"}
This is an invalid JSON, even when the "\\" instances are removed. Why is this happening?
Here is the code that handles the POST from Slack:
error_log(" -- dialog response: " . json_encode($_POST) . "\n", 3, './runtime.log');
Which results in the output above.
I'm not sure why you are calling json_encode($_POST). The documentation is very clear on the format that will be sent:
$payload = filter_input(INPUT_POST, 'payload');
$decoded = json_decode($payload);
var_dump($decoded);

Slack Message Dialog's Curl Call

can anyone please give me an example of slack message dialog in PHP? How to make curl call to dialog.open method? Please suggest.
Here is a simple example in PHP of how to create a Dialog based on a slash command.
Note that you need to create and install a Slack App first. You also need to add a slash command to your Slack app, which needs to call this script. Finally you need to manually add the OAuth Access Token from your Slack app to this script (replace YOUR_TOKEN).
The script will print the API response in the Slack channel, so you can the response and if they are any errors.
Execute the slash command to run the dialog.
// define the dialog for the user (from Slack documentation example)
$dialog = [
'callback_id' => 'ryde-46e2b0',
'title' => 'Request a Ride',
'submit_label' => 'Request',
'elements' => [
[
'type' => 'text',
'label' => 'Pickup Location',
'name' => 'loc_origin'
],
[
'type' => 'text',
'label' => 'Dropoff Location',
'name' => 'loc_destination'
]
]
];
// get trigger ID from incoming slash request
$trigger = filter_input(INPUT_POST, "trigger_id");
// define POST query parameters
$query = [
'token' => 'YOUR_TOKEN',
'dialog' => json_encode($dialog),
'trigger_id' => $trigger
];
// define the curl request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/dialog.open');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// set the POST query parameters
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));
// execute curl request
$response = curl_exec($ch);
// close
curl_close($ch);
var_export($response);

How can I upload a file to a Synology diskstation with PHP

I want to post to a Synology Disksation and upload a file with PHP. The Synology's API gives me an interface that I want to call with PHP. See the documentation below.
https://global.download.synology.com/download/Document/DeveloperGuide/Synology_File_Station_API_Guide.pdf#%5B%7B%22num%22%3A111%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C69%2C711%2C0%5D
Here is my code, which I use for the post:
<?php
$params = array(
'path' => '/home/upload',
'create_parents' => 'true',
'overwrite' => 'true',
'api' => 'SYNO.FileStation.Upload',
'version' => 2,
'method' => 'upload'
'_sid' => [id of session after authenticate],
'file[]' => "#".path_to_file
);
$ch = curl_init();
$BODY = http_build_query($params);
curl_setopt($ch, CURLOPT_URL, 'http://ip_of_diskstation:5000/entry.cgi');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $BODY);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
var_dump($result);
?>
This call works, but the diskstation gives me the following JSON.
{"error":{"code":401},"success":false}
According to the documentation, there is a "Unknown error of file operation". Also, using "file_get_contents (path to file)" instead of "#".path_to_file gives me the same error from the diskstation.
How can I post a file and parameters to a synology diskstation?
Can you try to add _sid in your web api url like http://ip_of_diskstation:5000/entry.cgi?_sid=
I make it work using nodejs request library.
Below is the code:
let r = request.post( { url: `${web_url}/entry.cgi?_sid=${sid}` }, ( err, response, body ) => {
return callback( err, response );
});
let form = r.form();
form.append( 'api', 'SYNO.FileStation.Upload');
form.append( 'version', '2');
form.append( 'method', 'upload' );
form.append( 'overwrite', 'false' );
form.append( 'path', '<shared folder path>');
form.append( 'create_parents', 'true' );
form.append( '_sid', sid );
form.append( 'file', fs.createReadStream( file ), { 'filename' : <filename without the path>});
Regards,
Felix
You get :
"{"error":{"code":401},"success":false}"
Because the official document is wrong.
See Upload file through FileStation upload api for details.
From what I see the url that you used in CURLOPT_URL needs to be updated. Also the file that is in the $params needs to be updated.
Some side notes. Make sure your settings for the user's SID has permissions set properly. Also make sure you updated the $_FILES["uploadfile"] to include whatever name you used within the input element of the form (instead of uploadfile, which I used).
See this github link for where I found my reference. Postman helped me duplicate this in PHP.
$sid = '_sid=[YOURSID]';
$url = 'HOSTNAME/webapi/entry.cgi?' . $sid;
$SynologySharedPath = 'FILEPATH';
$params = array(
'api' => 'SYNO.FileStation.Upload',
'version' => '2',
'method' => 'upload',
'path' => $SynologySharedPath,
'create_parents' => 'true',
'overwrite' => 'true',
'file' => new \CurlFile($_FILES["uploadfile"]["tmp_name"], $_FILES["uploadfile"]["type"], $_FILES['uploadfile']['name'])
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_VERBOSE,true);
$result = curl_exec ($ch);
$curlresponse = json_decode($result, true);

Square API Create Item working code now returning an error

After creating about 1000 inventory items with code, Square suddenly started returning an error.
The error returned was:
{"type":"bad_request","message":"'name' is required"}
Example code:
$postData = array(
'id' => 'test_1433281486',
'name' => 'test',
'description' => 'test',
'variations' => array(
'id' => 'test_v1433281486',
'name' => 'Regular',
'price_money' => array(
'currency_code' => 'USD',
'amount' => '19800',
),
),
);
$json = json_encode($postData);
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $access_token, 'Content-Type: application/json', 'Accept: application/json'));
curl_setopt($curl, CURLOPT_URL, "https://connect.squareup.com/v1/me/items");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
$json = curl_exec($curl);
curl_close($curl);
Here is the json_encoded string:
{
"id":"test_1433281486",
"name":"test",
"description":"test",
"variations": {
"id":"test_v1433281486",
"name":"Regular",
"price_money": {
"currency_code":"USD",
"amount":"19800"
}
}
}
I have tried everything I know to do a simple Create Item with code now, but it no longer works. Always returns the message "name" is required. My code to update images, fees, categories etc still works perfectly - just can't create.
I still have 100's of new inventory items to add, so getting this to work is imperative to business.
Variations needs to be passed in as an array. Here is how the JSON should look:
{
"id":"test_1433281486",
"name":"test",
"description":"test",
"variations": [{
"id":"test_v1433281486",
"name":"Regular",
"price_money": {
"currency_code":"USD",
"amount":"19800"
}
}]
}
Thanks for the report! We will update our error messaging to send the proper message if variations is not passed in as an array.
This is for all the PHP developers. Here is an updated Create Item PHP array that is compatible with Square. Notice the nested "variations" arrays.
Square now (6/15) requires brackets in JSON arrays - PHP json_encode() does not produce them unless you add nested arrays.
$postData = array(
'id' => 'test_1433281487',
'name' => 'test2',
'description' => 'test2',
'variations' => array(array(
'id' => 'test_v1433281487',
'name' => 'Regular',
'price_money' => array(
'currency_code' => 'USD',
'amount' => '19800',
),
)),
);
$json = json_encode($postData);
Here's another example of JSON brackets in PHP.
Here is the PHP json_encode() documentation.

Categories