cURL post request php to spring - php

I have to send array of a file to a spring server using PHP and cURL.
This is spring controller:
#RequestMapping(value = "/upload/teacher" ,method = RequestMethod.POST)
public HttpStatus uploadFiles(#RequestParam("files") MultipartFile[] inputFiles,
#RequestParam("assignmentID") String assignmentID) throws IOException { ... }
PHP cURL:
<?php
$filenames = array(pathfile1, pathfile2);
$postparameters = array(
'files' => array(
new CURLFile($filenames[0], "text/plain", pathinfo($filenames[0], PATHINFO_BASENAME)),
new CURLFile($filenames[1], "text/plain", pathinfo($filenames[1], PATHINFO_BASENAME))
),
'assignmentID' => "0008"
);
print_r($postparameters);
//init curl
$url = "http://localhost:8090/upload/teacher";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postparameters,
CURLOPT_HTTPHEADER => array(
"Content-Type: multipart/form-data"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
but with this code the backend does not receive anything.
if I send the curl request from the terminal, it works:
curl -F files=#"pathfile1","pathfile2" -F assignmentID="1" http://localhost:8090/upload/teacher
it also works on postman:
https://i.stack.imgur.com/Mo3yg.png]1
can someone tell me why if the backend does not receive the files when I run the request from php?

What happens if you annotate your spring controller method with #CrossOrigin ?
#CrossOrigin
#RequestMapping(value = "/upload/teacher" ,method = RequestMethod.POST)
public HttpStatus uploadFiles(#RequestParam("files") MultipartFile[] inputFiles,
#RequestParam("assignmentID") String assignmentID) throws IOException { ... }

Related

How to invoke a function that accepts Request Object from another function?

In a PHP OAuth Implementation, there is a function as below:
/**
* Processes POST requests to /oauth/token.
*/
public function token(ServerRequestInterface $request) {
// Extract the grant type from the request body.
$body = $request->getParsedBody();
$grant_type_id = !empty($body['grant_type']) ? $body['grant_type'] : 'implicit';
/*.. CODE TRIMMED ..*/
catch (OAuthServerException $exception) {
watchdog_exception('simple_oauth', $exception);
$response = $exception->generateHttpResponse(new Response());
}
return $response;
}
I think its an instance of:
Psr\Http\Message\ServerRequestInterface;
I want to call this function, and pass the body params to this function from another function. Something as below:
public function call_token(){
//Something like this??
$request = new ServerRequestInterface();
$request->setUrl('https://some.url/oauth/token');
$request->setMethod("POST");
$request->setHeader(array(
'Content-Type' => 'application/x-www-form-urlencoded',
));
$request->addPostParameter(array(
'grant_type' => 'password',
'client_id' => '828472a8-f2c5-4e79-a158-ab041d3b313a',
'client_secret' => 'secret',
'username' => 'admin',
'password' => '123'
));
$response = $token($request);
}
I am not able to figure out how we can call this function.
This is the full source code where the token code is implemented. I am trying to call this function from another class.
Below is a working CURL request of how requests are made to this endpoint. I however want to emulate the request from within the application.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://some.url/oauth/token',
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 => 'grant_type=password&client_id=828472a8-f2c5-4e79-a158-ab041d3b313a&client_secret=secret&username=admin&password=123',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

cURL: wait for response until it is processed by api

everybody,
I am using the following code for synchronizing data with an API ($method= POST) and then I call using the GET method ($method= GET) to print the result of the synchronization:
function sendCurl ($fileName, $method)
{
$curl = curl_init();
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 => $method,
CURLOPT_POSTFIELDS => array('file' => new CURLFILE($fileName)),
CURLOPT_HTTPHEADER => array(
"Authorization: xxxxxxxxxxxxxxx",
"Accept: application/json."
"Content-Type: multipart/form-data"
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
The problem is that the GET method response arrives before the API has time to process it. Therefore, when I print the response I print WAITING FOR RESPONSE and not whether it went ok or failed.
What can I do to print the ok or fail and not the WAITING FOR RESPONSE?
Thank you very much!

SOAP data returns from Postman but not with Guzzle

Whenever I use Postman to make a soap post request, I get back the desired data. Using Guzzle, no data is returned. Im new to SOAP and using the resources online to go along.
I have omitted the variables for ease of read.
$xml = (
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:app="<ACTION-URL>">
<soapenv:Body>
<app:' . $functionName . '>
<request>'
. $requestBody .
'<authentication>
<username>'. $authObject->username .'</username>
<password>'. $authObject->password .'</password>
<user_id>'. $authObject->userId .'</user_id>
<dealer_id>'. $authObject->clientBranchId .'</dealer_id>
</authentication>
</request>
</app:' . $functionName . '>
</soapenv:Body>
</soapenv:Envelope>'
);
private function makeSOAPRequest($xml)
{
$client = new \GuzzleHttp\Client();
$options = [
'headers' => [
'Content-Type' => 'text/xml; charset=utf-8'
],
'body' => $xml,
'Authenticate' => [env('USERNAME'), env('PASSWORD')]
];
$url = env('ROSETTA_API');
$promise = $client->requestAsync('POST', $url, $options);
$response = $promise->wait();
$xml = simplexml_load_string($response->getBody(),'SimpleXMLElement',LIBXML_NOCDATA);
if ($xml) {
$json = json_encode($xml);
return json_decode($json, true);
}
return false;
}
Calling makeSOAPRequest(..), I get back false. Using the same xml data in Postman, data is returned. Is there anything I've missed in the request header?
Edit to use cURL
// Copied from Postman:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://<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 => $xml,
CURLOPT_HTTPHEADER => array(
"Cookie: PHPSESSID=6419f1d96025a4a2c4d454de33fc6820"
),
));
$response = curl_exec($curl);
curl_close($curl);
return $response; // xml data in string format.
cUrl does work but not Guzzle. How to return as json? When I use simplexml_load_string, it's empty.
Try to use below parameters in your curl:
CURLOPT_SSL_VERIFYPEER => 1
CURLOPT_POST => true
// IF NEEDED
CURLOPT_HTTPAUTH => CURLAUTH_ANY
// IF NEEDED
CURLOPT_USERPWD => $soapUser.":".$soapPassword)
For Checking Error After curl_exec and before curl_close
if( curl_errno($ch) ){
print_r(curl_error($curl));
}
In add these headers in your header if needed:
"accept" => "*/*",
// IF NEEDED
"accept-encoding" => "gzip, deflate"
Add your request code in try catch block:
try{
//request code
}
catch(Exception $e){
print_r($e->getCode());
print_r($e->getMessage());
}

How to consume my webservice with PHP

I have a question ..
My app gives me the following information:
HTTP + JSON
The following are sample HTTP requests and responses. The placeholders shown need to be replaced with actual values.
POST /json/reply/Zona
HTTP/1.1
Host: equatepro.azurewebsites.net
Content-Type: application/json
Content-Length: length
{"zonaId":0,"nombre":"String","creadoPor":"String","creadoFecha":"/Date(-62135596800000-0000)/","modificadoPor":"String","modificadoFecha":"/Date(-62135596800000-0000)/","estado":"String","nota":"String","borrar":false}
AND then
The following routes are available for this service:
POST /api/zonas
PUT /api/zonas/{zonaId}
enter image description here
enter image description here
I'm trying to communicate with my webservice using PUT method
My code
<?php
$pantalla="zonas";
%id =8;
$url= "http: //miapp.com /api/zonas/8".$pantalla ;
$url = $url ."/" . $id;
// complete url http://miapp.com/api/zonas/8
//build json
$ConstructorJson = array(
'ZonaId' => $Datos['txt_codigo'],
'Nombre' => $Datos['txt_Nombre'],
'CreadoPor' => $Datos['txt_CreadoPor'],
'CreadoFecha' => $Datos['txt_CreadoFecha'],
'ModificadoPor' => $Datos['txt_ModificadoPor'],
'ModificadoFecha' => $Datos['txt_ModificadoFecha'],
'Estado' => $Datos['cbo_Estado'],
'Nota' => $Datos['txt_Notas']
);
$json = json_encode($ConstructorJson);
$opts = array(
"http" => array(
"method" => "PUT",
"header" => "Accept: application/xml\r\n",
"content" => $json
)
);
$context = stream_context_create($opts);
$response = file_put_contents($url,'8',false,$context);
?>
Give me the following error
Warning: file_put_contents(http: //miapp .com/api/zonas/8): failed to open >stream: HTTP wrapper does not support writeable connections in C:\xampp\htdocs\Codigo2.0\models\zonas.model.php on line 34
and nothing happens.
I would rather connect using PHP curl.
$ConstructorJson = array(
'ZonaId' => $Datos['txt_codigo'],
'Nombre' => $Datos['txt_Nombre'],
'CreadoPor' => $Datos['txt_CreadoPor'],
'CreadoFecha' => $Datos['txt_CreadoFecha'],
'ModificadoPor' => $Datos['txt_ModificadoPor'],
'ModificadoFecha' => $Datos['txt_ModificadoFecha'],
'Estado' => $Datos['cbo_Estado'],
'Nota' => $Datos['txt_Notas']
);
$json = json_encode($ConstructorJson);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http: //miapp.com/api/zonas/8/zonas",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $json
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"Accept: application/xml\r\n",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
HTTP wrapper does not support writeable connections - basically, PHP is telling you "Hey, you can't use this function to write to a file that lives on the internet. How do you expect me to write a file # http: //miapp .com/api/zonas/8? Not gonna happen".
I assume what you're trying to do is to send a PUT request # that location to update a zonas resource with ID 8.
Solution
Consider using a proper HTTP client that can send actual HTTP request methods and conform to the HTTP spec.
My personal favourite inside PHP is Guzzle -
http://docs.guzzlephp.org/en/stable/. Guzzle is a standalone package and can be downloaded from their site. You can use it in any PHP project - without or without a framework.
With Guzzle, you'd do something like the following:
$client = new GuzzleHttp\Client();
$json = json_encode($ConstructorJson);
$headers = [
"Accept" => "application/xml\r\n"
]
$request = new Request('PUT', $url, ['body' => $json, 'headers' => $headers]);
$client->send($request);
I finally solved the problem (thank you Kyle O'Brien)
Code
<?php
// web service url + tabla + id
$url = "mywebservice.com/zonas/8";
$Datos = $_POST;
//create a array with dates
$ConstructorJson = array(
'Nombre' => $Datos['txt_Nombre'],
'CreadoPor' => $Datos['txt_CreadoPor'],
'CreadoFecha' => $Datos['txt_CreadoFecha'],
'ModificadoPor' => $Datos['txt_ModificadoPor'],
'ModificadoFecha' => $Datos['txt_ModificadoFecha'],
'Estado' => $Datos['cbo_Estado'],
'Nota' => $Datos['txt_Notas']
);
//convert array to json
$json = json_encode($ConstructorJson);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => array(
'Accept: application/json',
'Content-Type: application/json',
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>

Dropbox HTTP API - PHP cURL added header item boundary automatically

I'm new with the Dropbox API integrations, and I'm using the PHP cURL extension to make calls to the HTTP REST API, and when I try to make a request I receive the following string:
Error in call to API function "files/list_folder":
Bad HTTP "Content-Type" header:
"text/plain; boundary=----------------------------645eb1c4046b".
Expecting one of "application/json", "application/json; charset=utf-8",
"text/plain; charset=dropbox-cors-hack".
I'm sending this with code very similar to this:
$sUrl = "https://api.dropboxapi.com/2/files/list_folder";
$oCurl = curl_init($sUrl);
$aPostData = array('path' => '', 'recursive' => true, 'show_hidden' => true);
$sBearer = "MY_TOKEN";
$aRequestOptions = array(
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array('Content-Type: text/plain',
'Authorization: Bearer ' . $sBearer),
CURLOPT_POSTFIELDS => $aPostData,
CURLOPT_RETURNTRANSFER => true);
curl_setopt_array($aRequestOptions);
$hExec = curl_exec($oCurl);
if ($hExec === false){
// Some error info in JSON format
} else {
var_dump($hExec);
}
As you have it, you're doing a multipart form upload, which isn't what the API expects.
There are a few things you need to do differently:
You should be sending up the parameters as JSON in the body.
You should set the Content-Type to application/json, accordingly.
There isn't a show_hidden parameter on /files/list_folder, but perhaps you meant to send include_deleted.
The curl_setopt_array method takes two parameters, the first of which should be the curl handle.
Here's an updated version of your code that works for me:
<?php
$sUrl = "https://api.dropboxapi.com/2/files/list_folder";
$oCurl = curl_init($sUrl);
$aPostData = array('path' => '', 'recursive' => true, 'include_deleted' => true);
$sBearer = "MY_TOKEN";
$aRequestOptions = array(
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array('Content-Type: application/json',
'Authorization: Bearer ' . $sBearer),
CURLOPT_POSTFIELDS => json_encode($aPostData),
CURLOPT_RETURNTRANSFER => true);
curl_setopt_array($oCurl, $aRequestOptions);
$hExec = curl_exec($oCurl);
if ($hExec === false){
// Some error info in JSON format
} else {
var_dump($hExec);
}
?>

Categories