Specifying fields in API using PHP - php

I have the following code which works;
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.nutritionix.com/v1_1/search");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"appId=id&appKey=key&query=milk");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec ($ch);
curl_close ($ch);
print_r($response);
?>
Question is, how do i specify fields to be outputted in the results?
I have tried, "appId=id&appKey=key&query=milk&fields=item_name, nf_calories" but to no avail..
Any help appreciated. Thanks.

Simply adding a question mark in between should do the trick really:
https://api.nutritionix.com/v1_1/search?appId=id&appKey=key&query=milk&fields=item_name
And so on for the field names.
I've generally used this approach when using curl, you can take a look at it if you want:
function curl_get_contents($url)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
function getUrl()
{
$url = "https://api.nutritionix.com/v1_1/search?appId=id&appKey=key&query=milk&fields=item_name";
return $url;
}
function getData()
{
$i = 0;
$url = getUrl();
$response = curl_get_contents($url);
$data = json_decode($response);
if(!isset($data->data))
{
die("no data.");
}
$data = $data->data;
return $data;
}
EDIT: Try an url like this:
https://api.nutritionix.com/v1_1/search/milk?results=0:20&fields=item_name,brand_name,item_id,nf_calories&appId=APPID&appKey=APPKEY

Related

PHP cURL response issue

I'm using the below curl request which returns a XML structured response. The response however seems wrong (first line: string(744978) "<?xml etc). I normally json_decode it, however that does not seem to work. Is this an issue in the endpoint or am I doing something wrong? I would like to convert the response to an array so I can store it in a database.
REQUEST
$url = 'url';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
RESPONSE
use this function for result like
$url = 'url';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$result = api_result($result);
curl_close($ch);
function api_result($result){
$plainXML = $this->mungXML($result);
$arrayResult = json_decode(json_encode(SimpleXML_Load_String($plainXML, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $arrayResult;
}
<!-- begin snippet: js hide: false console: true babel: false -->
function xml_to_json_feed() {
$url = 'http://x';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$xml = simplexml_load_string($result);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
curl_close($ch);
return $array;
}

PHP cURL convert to GuzzleHttp

Please, how to convert the following code to async GuzzleHttp?
In this way, php is waiting for the return of each query.
while ($p = pg_fetch_array($var)) {
$url = "https://url";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array("Content-Type: application/json");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = '{"s1":"s1","number":"'.$p['number'].'"}';
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
}
Here's a working example using native curl_multi_*() functions. Specifically:
curl_multi_init()
curl_multi_add_handle()
curl_multi_exec()
curl_multi_getcontent()
curl_multi_remove_handle()
curl_multi_close()
<?php
$urls = ['https://example.com/', 'https://google.com'];
$handles = [];
foreach ($urls as $url) {
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$handles[] = $handle;
}
$multiHandle = curl_multi_init();
foreach ($handles as $handle) {
curl_multi_add_handle($multiHandle, $handle);
}
for(;;){
curl_multi_exec($multiHandle, $stillRunning);
if($stillRunning > 0){
// some handles are still downloading, sleep-wait for data to arrive
curl_multi_select($multiHandle);
}else{
// all downloads completed
break;
}
}
foreach ($handles as $handle) {
$result = curl_multi_getcontent($handle);
var_dump($result);
curl_multi_remove_handle($multiHandle, $handle);
}
curl_multi_close($multiHandle);
Now, how convert the code cUrl to GuzzleHttp?

PHP curl get multidimensional array

I'm trying to get a list of issues using gitlab API. The response is correct testing the URL, but the curl doesnot execute in my php code. I guess it`s because of the multidimensional array. but how do you solve this? Here is my function:
function getissues_list () {
$url = MAIN_URL."/issues";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPGET , true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->token);
$response = curl_exec($ch);
if (!curl_exec($ch)) {
echo ($this->exception_message);
} else{
return $response;
curl_close($ch);
}
}
Thanks!
function getissues_list () {
global MAIN_URL; // Is this accessible in this function?
$url = MAIN_URL."/issues";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPGET , true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->token);
$response = curl_exec($ch);
var_dump($response);// ADD THIS LINE TO DEBUG!!!!!!!!
if (!curl_exec($ch)) {
echo ($this->exception_message);
} else{
return $response;
curl_close($ch);
}
}

php curl request returns nothing

i am using codeigniter and my code is as follows :
Controller :
$json_string = 'http://maps.googleapis.com/maps/api/directions/json?origin=' . $user_addr . '&destination=' . $vendor_city .'&mode=Montreal';
$json = $this->curl_get_contents($json_string);
$obj = json_decode($json, true);
$this->load->view('get_direction_pg',$obj);
public function curl_get_contents($url)
{
$ch = curl_init($url);
if ($ch == FALSE) {
return array('error' =>"failed");
}else{
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$result = curl_exec($ch);
return $result;
}
}
and it if i access var_dump($routes);
it says,
undefined variable routes.
try using this: i.e. encoding the vars - before putting into url
$json_string = 'http://maps.googleapis.com/maps/api/directions/json?origin=' . urlencode($user_addr) . '&destination=' . urlencode($vendor_city) .'&mode=Montreal';
the parameters should be encoded for url, otherwise it is being invalid, and hence you are getting that error
Try this:
public function curl_get_contents($url)
{
$ch = curl_init($url);
if ($ch == FALSE) {
return array('error' =>"failed");
}
else
{
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$result = curl_exec($ch);
return $result;
}
}

Can't use file_get_contents on https://api.facebook.com

I am trying to get some information using https://api.facebook.com
The following works just fine: (notice graph.facebook.com)
$q = "https://graph.facebook.com/$params".$sep."access_token=$access_token";
$response = json_decode(file_get_contents($q),true);
But
$q = "https://api.facebook.com/$params".$sep."access_token=$access_token";
$response = json_decode(file_get_contents($q),true);
Just does not work. I've tried curl, but I keep getting Method Not Implemented
Following code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$url = "https://api.facebook.com/$params".$sep."access_token=$access_token";
echo $url;
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_URL, $url);
echo curl_exec($ch);
echo curl_errno($ch);
curl_close ($ch);
Any ideas?
Such a silly mistake. I post the working example:
The call:
$status = $fb->api("method/fql.query?query=".urlencode("SELECT url FROM url_like WHERE user_id = $ID")."",$access_token);
The class:
class fb
{
public function api($params, $access_token)
{
try
{
$q = "https://api.facebook.com/$params?format=json-strings&access_token=$access_token";
$response = json_decode(file_get_contents($q),true);
if ($response->error->message != null)
throw new Exception($response->error->message);
else
return $response;
}
catch (Exception $e)
{
return ("<b>Error:</b> " . $e->getMessage());
}
}
}
For everyone who don't want to use the facebook php sdk.

Categories