I have a RESTful server at server.pad.com/authenticate (locally) that takes one parameter and returns JSON. So in Laravel its authenticate/(:any)
I'm trying to get data from an ajax request and send it to the server and send back the response. Heres what I've tried...
<?php
$json = json_decode($_POST['data'], true);
$url = 'http://service.pad.com/authenticate';
$curl = curl_init($url);
$data = json_encode($json);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
curl_close($curl);
echo json_encode($response);
?>
Probably Content-Type: application/x-www-form-urlencoded
issue.
Your $data
is in JSON. You set value with CURLOPT_POSTFIELDS, but not var.
As I can see, $_POST['data'] is in JSON.
In the case the server gets single data
in JSON try this:
$url = 'http://service.pad.com/authenticate';
$curl = curl_init($url);
$postdata = array( 'data' => $_POST['data'] );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postdata) );
$response = curl_exec($curl);
curl_close($curl);
echo json_encode($response);
But if the server gets multiple vars, not JSON, try this:
$url = 'http://service.pad.com/authenticate';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS,
http_build_query( json_decode($_POST['data'], true) )
);
$response = curl_exec($curl);
curl_close($curl);
echo json_encode($response);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.