cURL HTTP Request with PHP (Routific API) - php

I'm trying to make an HTTP Request in PHP using cURL. The Request goes to the Routific (Route Optimization) API -> https://docs.routific.com/v1.3.1/docs
The idea is to send a JSON with different waypoints and get an optimal route back. Unfortunately, I get the following error when executing the code:
{"error":"Missing network"}
My Code looks as follows:
<?php
$data_string = json_encode(file_get_contents('Routes.json'));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.routific.com/v1/vrp");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: bearer API_KEY')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$result = curl_exec($ch);
echo $result;
curl_close($ch);
?>
What is wrong?
Thanks!

Related

Google Drive trash Clear Using Php not works

I am trying to clear Trash using php and drive api v3. Code excutes with no error but trash in drive is not deleted. Here is my code please help me to fix this. Thanks For your time.
if($_POST['trash']) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/files/trash?key='.$token.'");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Authorization: Bearer ' . $token
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
};
I believe your goal and current situation as follows.
You want to use "Files: emptyTrash" in Drive API v3 using curl of php.
Your access token of $token can be used for using this API.
In order to achieve your goal, how about the following modification?
From:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/files/trash?key='.$token.'");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Authorization: Bearer ' . $token
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
To:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/drive/v3/files/trash");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $token));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
Note:
At "Files: emptyTrash", no values are returned.
If $token in your script is the API key which is not the access token, please retrieve the access token and use it. Please be careful this.
Reference:
Files: emptyTrash

PHP cURL how to send a JSON POST request and also include a URL querystring?

I'm trying to submit a POST request with JSON data to an api endpoint. The endpoint requires a querystring passing the api credentials, but also requires the JSON data to be POSTed.
When I try to do this with PHP cURL as shown below, the querystring is apparently removed - thus the api is rejecting the request due to missing api key.
I can do this easily with Postman when testing access to the api endpoint.
How can I make the cURL request include both the querystring AND the JSON POST body?
Example code:
// $data is previously defined as an array of parameters and values.
$url = "https://api.endpoint.url?api_key=1234567890";
$ch = curl_init();
$json = json_encode($data);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($json)
]
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
You are doing almost right.
Sometimes you need to relax SSL verification.
Otherwise, update php ca bundle:
https://docs.bolt.cm/3.7/howto/curl-ca-certificates
Add the following:
$headers = array(
"Content-type: application/json;charset=UTF-8",
"Accept-Encoding: gzip,deflate",
"Content-length: ".strlen($json),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, "identity, deflate, gzip");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Sometimes you need to change encoding too:
$result = utf8_decode($result);
And check the returning data.

PHP POST to API with JSON Body and Auth

I am in the process of creating a super simple PHP page that sends JSON data to an remote API via POST. I am having issues with it and hoping someone here could help me out.
This is what I have so far
$url = "https://remoteHost.com/api/post";
$json = '{"message":"textHere", "user":"userABC"}';
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Basic --ACB123-ABC123---ABC123---',
'Content-Type: application/json',
'Content-Length: ' . strlen($json),)
);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
When I execute this nothing happens. The page loads without errors but it looks like the curl isn't actually doing anything. Any Ideas?

Trivia API not returning proper JSON in PHP

I am using this API
https://market.mashape.com/pareshchouhan/trivia
for making widget in my site but when i am calling this API through CURL it is returning following error:
Any solution for that?
Here my code :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://pareshchouhan-trivia-v1.p.mashape.com/v1/getAllQuizQuestions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
curl_setopt($ch,CURLOPT_HEADER,FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"X-Mashape-Key" => "MY_KEY",
"Accept" => "application/json"
));
$data = curl_exec($ch);
curl_close($ch);
echo $data;
when i execute this PHP file, it is returning "Missing Mashape application key" in return data.
http://i.prntscr.com/5f98b1e8965f42eda29875543b052ec5.png
You are specifying you curl headers incorrectly. Look at the CURLOPT_HTTPHEADER option in the docs at http://php.net/manual/en/function.curl-setopt.php. Your code should look like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://pareshchouhan-trivia-v1.p.mashape.com/v1/getAllQuizQuestions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"X-Mashape-Key: MY_KEY",
"Accept: application/json"
));
$data = curl_exec($ch);
curl_close($ch);

when CURLOPT_HTTPHEADER need 'Content-Length'

I have this code in the client side of my application:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length:0 ' )
);
$body = curl_exec($ch);
while in the server files:
$ch = curl_init($this->modelAddress);
$data = array("params"=>array("resource" => "artist","operation" => $operation,"params"=>$params));
$data_string = json_encode($data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
They are made from 2 different people and I want to ask what is the most correct.
When the use of 'Content-Length:0' instead of 'Content-Length:'.strlen($data_string) is better.
It's only question of POST/GET method?
I've searched explanations of this in the web but I've found nothing
If you use http PUT with curl then you have to specify the Content-Length header. Otherwise it is automatically handled by the curl for both GET and POST.
For example if you are posting the following data with curl with CURLOPT_POSTFIELDS. Then curl will automatically add the length of the data with the header.
$data = "name=alex&email=none!";
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
For better understand, run your curl code using verbose mode and you'll see how its handling the requests and responses.
curl_setopt($ch, CURLOPT_VERBOSE, true);

Categories