Api.ai API for php (with REST API by POSTing) - php

llo,
I'm trying to integrate my webapp with RESTful api in php. This requires me to POST data to https://api.api.ai/v1/query and also have 2 custom headers.
my current code is
$data = array("v" => "20150821", "query" => "whats the time", "lang" => "EN", "sessionId" => "1234567890");
$data_string = json_encode($data);
$ch = curl_init('https://api.api.ai/v1/query');
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),
'Content-type: application/json',
'Authorization: Bearer XxxX-xxx',
'ocp-apim-subscription-key: XxxX-xxx')
);
$result = curl_exec($ch);
If i run this, (with my real auth and key obv) i get nothing. Like Blank. Any ideas?

I AM SO SORRY.
I havent been sleeping well and did a rather idiotic mistake
i forgot to echo $result
i know, i know. I'll have to be a bit more careful
Thanks anyway :P

Related

Need example of OAuth 2.0 access in PHP to Google Shopping API using ONLY CURL

I have a PHP-7 application which needs to access the Google Shopping API (OAuth2.0 ...) from the back-end servers. In other words, it must use CURL. I am looking for as complete an example as possible – from any source.
I do not want to use Google's code which first of all doesn't seem to use CURL and second of all seems to involve over 7,000 new PHP source-files.
Many APIs seem to reference $_GET[] as though the requesting agent was itself "a web page." In this case, it is not: the requests are being made using CURL, and must be so.
This should work
$data = array("post" => $vars, "post2" => $vars2);
$data_string = json_encode($data);
$ch = curl_init('https://endpoint.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $oauth_key,
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$output = curl_exec($ch);

Street view publish api pose.level won't save

I'm trying to post photo.pose.level on existing photo using Street View Publish API.
When i post GPS, HEADING and LEVEL data, first two (GPS+HEADING) are stored on street view photo, but LEVEL data is lost and won't save (server response is OK).
Here is my POST code that works for HEADING and GPS, i can't figure out where i'm wrong or if i'm missing something.. i think i've done all as described in documentation
$data['pose']['latLngPair']['latitude']=$lat+0;
$data['pose']['latLngPair']['longitude']=$lon+0;
$data['pose']['heading']=$heading+0;
$data['pose']['level']['number']=$level+0;
$data['pose']['level']['name']='L'.($level+0).'';
$data_string = json_encode($data);
$ch = curl_init('https://streetviewpublish.googleapis.com/v1/photo/'.$photo_id.'?key='.$_GOOGLE_API['api_key'].'&updateMask=pose.latLngPair,pose.heading,pose.level');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'authorization: Bearer '.$_GOOGLE_API['access_token'],
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$json_response=curl_exec($ch);
curl_close ($ch);
Looks good. How long did you wait? Sometimes it takes up to 24hrs before the data shows up in the getPhoto requests.

how to make a get call with JSON and retrieve data

I am trying to follow instructions from an RFC spec to retrieve data from "certificate transparency log" known logs here
Instructions say to make a JSON call but i don't seem to be having any success.. i actually don't get any data response at all.
Here is the guide I am trying to follow:
https://www.rfc-editor.org/rfc/rfc6962#section-4.6
Here is the php code I am using
<?php
$data = array("start" => "1", "end" => "10");
$data_string = json_encode($data);
$ch = curl_init('https://ct.googleapis.com/pilot/ct/v1/get-entries');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
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))
);
$result = curl_exec($ch);
var_dump(json_decode($result, true));
?>
The service I was connecting to has an SSL certificate installed and was not responding to my requests. Disabling the trust verification was the solution. I added this to the CURL options CURLOPT_SSL_VERIFYPEER => false

How to use variables in a curl post?

I am creating a php script that will use curl to add a new row in my Parse database. I am having difficulty adding variables to my setopt statement. If anyone could lead me in the right direction, then I would greatly appreciate it.
This is my PHP code that is executing a curl statment:
//curl commands to send information to parse
$ch = curl_init('https://api.parse.com/1/classes/className');
curl_setopt($ch,CURLOPT_HTTPHEADER,
array('X-Parse-Application-Id:secret1',
'X-Parse-REST-API-Key:secret2',
'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"Name\":\"$deviceName\"}" );
echo curl_exec($ch);
curl_close($ch);
The response that I get back is:
{"code":107,"error":"invalid JSON"}
Thank you in advance!
Is there any reason you can't encode an array before sending the data?
For Example (untested):
$arr = [ "Name" => $deviceName ];
$arr_string = json_encode($arr);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($arr_string)
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $arr_string );

fulfillment of orders via shopify API

i have a test shop on shopify. When the orders are created I want to update their fulfilment status to "Fulfilled" via PHP API calls.
So far nothing is mentioned in the documentation to update the fulfilment status of the line items in the order.
Is there any way to do so?
Yes. API Fulfillment is possible: http://api.shopify.com/fulfillment.html
This is how I have it working now
$data = array( "fulfillment" => array("tracking_number" => "1111111111"));
$data_string = json_encode($data);
$ch = curl_init('https://API:PASS#STORE.myshopify.com/admin/orders/ORDER_ID/fulfillments.json');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);

Categories