I’m new to PHP and trying to make a request to a third-party API using curl. This is what I am trying, but it is just responding with what is on the API’s root endpoint.
$service_url = 'https://api.kivaws.org/graphql';
$curl = curl_init($service_url);
$curl_post_data = array(
'Pragma' => 'no-cache',
'Origin' => 'https://api.kivaws.org',
'Accept-Encoding' => 'gzip, deflate',
'Accept-Language' => 'en-US,en;q=0.8',
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
'content-type' => 'application/json',
'accept' => 'application/json',
'Cache-Control' => 'no-cache',
'Connection' => 'keep-alive',
'data' => '{"query":"{
loans (filters: {gender: male, status:funded, country: ["KE", "US"]}, sortBy: newest, limit: 2) {
totalCount
values {
name
status
loanAmount
}
}
}","variables":null,"operationName":null}'
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
echo $curl_response;
5
1 Answer
This seems to be doing the trick: using curl_setopt
to set few important things, like CURLOPT_HTTPHEADER
, CURLOPT_RETURNTRANSFER
to True
so that it will return us the result in the body, and then encoding the query using json_encode
.
$service_url = 'https://api.kivaws.org/graphql';
$curl = curl_init($service_url);
$curl_post_data = array("query" => '{loans (filters: {gender: male, status:funded, country: ["KE", "US"]}, sortBy: newest, limit: 2) {totalCount values { name status loanAmount }}}');
$data_string = json_encode($curl_post_data);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additional info: ' . var_export($info));
}
curl_close($curl);
echo $curl_response;
echo $info;
This returns me:
{"data":{"loans":{"totalCount":40425,"values":[{"name":"Davis","status":"funded","loanAmount":"100.00"},{"name":"James","status":"funded","loanAmount":"100.00"}]}}}
1
-
awesome. Thanks a ton.
– BrettAug 14, 2017 at 7:07
Can You pls post what is the end-point response?
Aug 14, 2017 at 5:31
And what desired output you want?
Aug 14, 2017 at 5:31
hello @Brett I got this reponse from your Api curl is this correct? {"data":{"hello":"Welcome to Kiva's GraphQL API!"}}
Aug 14, 2017 at 5:31
Yes, that is what I'm getting. I want to be getting a json response of the loans specified in the data section of the query.
Aug 14, 2017 at 5:37
@Brett I got this :- error occured during curl exec. Additioanl info: 1
Aug 14, 2017 at 5:55