So having a small issue sending a file to API using cURL
This is the given example that works
curl -X POST \
--header "Content-Type:application/json" \
-d @trace.json \
"https://api.mapbox.com/matching/v4/mapbox.driving.json"
This return me the accurate data.
What I am trying to achieve is the same but using vanilla PHP
function sendRequest() {
// initialise the curl request
$request = curl_init('https://api.mapbox.com/matching/v4/mapbox.driving.json');
// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
array(
'header' => 'Content-Type:application/json',
'file' => '@' . realpath('trace.json')
));
// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
// close the session
curl_close($request);
}
sendRequest();
I have tried this, but this returns me a invalid input error, although the input is copied from the example.
I have a feeling that its either down to the CURLOPT_POSTFIELDS
that I am sending or the whole structure is wrong.
Use CURLOPT_HTTPHEADER
, and not pass headers to POST
data, like this:
curl_setopt($request, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json'
));
curl_setopt($request, CURLOPT_POSTFIELDS, array(
'file_contents' => '@' . realpath('trace.json')
));
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.