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