PHP Curl with Knack API - Post an image problem - php

Does any here have experience with Knack API(https://www.knack.com/) here?
I'm using PHP curl to post an image.
When I use POSTMAN, the image successfully uploads. But when I copy the code from POSTMAN(postman option to copy the source code) and add it to my source code, knack returns empty object error("Please select a file to upload").
Knack API documentation: https://www.knack.com/developer-documentation/#file-image-uploads
The POSTMAN source code below
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.knack.com/v1/applications/app-id/assets/file/upload",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"files\"; filename=\"C:\\Users\\shavk\\Pictures\\292937.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Content-Type: application/x-www-form-urlencoded",
"Postman-Token: fe1f4eea-b2b5-2d43-2c3c-c262438866d4",
"X-Knack-Application-Id: id",
"X-Knack-REST-API-Key: key",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
Any help is highly appreciated.

Postman's PHP/curl code generator isn't very good. to upload files in the multipart/form-data-format, use CURLFile.
the code should be
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.knack.com/v1/applications/app-id/assets/file/upload",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'files' => new CURLFile("C:\\Users\\shavk\\Pictures\\292937.jpg")
),
CURLOPT_HTTPHEADER => array(
"X-Knack-Application-Id: id",
"X-Knack-REST-API-Key: key",
),
));
$response = curl_exec($curl);
if you are a customer of Postman (using the pro/whatever version), i suggest you make a complaint to customer support about the terrible code generated by the PHP code generator.

Related

How to get WordPress posts count using PHP Curl

I'm using php curl API to fetch the posts from WordPress. But I'm not getting X-WP-Total or X-WP-TotalPages keys with response to work with pagination.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com/wp-json/wp/v2/posts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => [
'Accept: application/json',
"Content-Type: application/json",
"Access-Control-Allow-Origin: *",
"Access-Control-Allow-Methods: GET, POST, OPTIONS",
"Access-Control-Allow-Headers: X-Requested-With"
],
));
$response = curl_exec($curl);
$response = json_decode($response);
// print_r($response);exit;
curl_close($curl);

PHP Curl GET request not working but works fine in POSTMAN

I am trying to get product data from google merchant center (google shopping ), my curl command works fine on postman, but not on php. Please help me
On postman:
On Php:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://shoppingcontent.googleapis.com/content/v2.1/436306321/products',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ya29.a0ARrdaM8TK4rZeVRnsj9MgcFg3STE1EqGvwj20J-REq8f6poCzGIv2MzUIegdSP1DDa_-HQpHIQNBTU6Rnxf7HsnWCjwkP7_pwkp-kr5zm0o0Z6EuZCVa4IdwHzX7MGM8SQ-94Vkxps4JRSbkgplYo4w1K8lPwQ'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;

getting auth error while calling search date api using production environment

I am able to generate access_token from production environment by given below code
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.amadeus.com/v1/security/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "client_id=XXX&client_secret=XXX&grant_type=client_credentials",
CURLOPT_HTTPHEADER => array(
// Set here requred headers
"accept: */*",
"accept-language: en-US,en;q=0.8",
"content-type: application/x-www-form-urlencoded",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
after that i am trying to call
https://api.amadeus.com/v1/shopping/flight-dates?origin=MAD&destination=MUC
API. By using following code
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.amadeus.com/v1/shopping/flight-dates?origin=JFK&destination=LHR&oneWay=false&nonStop=false",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
// Set here requred headers
"accept: */*",
"accept-language: en-US,en;q=0.8",
"content-type: application/x-www-form-urlencoded",
"Authorization: Bearer XXX",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
i am getting following error
{
"errors": [
{
"status": 401,
"title": "Wrong authentication credentials.",
"code": 701,
"source": {
"pointer": "shopping/flight-dates"
}
}
]
}
what am i doing wrong.
We had an internal configuration issue that has been fixed everything should work fine now.
2 things:
This API doesn't support airport codes you will need to replace them by city codes (LHR-> LON / JFK -> NYC), this is part of our backlog to support airport codes as well but not delivered yet. So API call should be:
https://api.amadeus.com/v1/shopping/flight-dates?origin=NYC&destination=LON&oneWay=false&nonStop=false
The only needed header is:
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer XXX",
),

Update a Page with PHP Variables / REST API Confluence

I want to update a page in confluence with some php-variables. So here's my PHP Code to update the page:
$curl = curl_init();
$post = "{\"id\":\"65604\",\"type\":\"page\",\"title\":\"page\",\"space\":{\"key\":\"***\"},\"body\":{\"storage\":{\"value\":\"<p>Here comes the other variable: $product_response </p>\",\"representation\":\"storage\"}},\"version\":{\"number\":11}}";
curl_setopt_array($curl, array(
CURLOPT_PORT => "6003",
CURLOPT_URL => "http://localhost:6003/rest/api/content/65604",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $post,
CURLOPT_COOKIE => "JSESSIONID=3A16CBFE8B99E619D62BD4CD6573F184",
CURLOPT_HTTPHEADER => array(
"authorization: Basic xyYS123_test_test-45Sdasds==",
"content-type: application/json"
),
));
For your information:
If i print the $post in another script, the variable value show up
without the variable, the curl session works and I can update the page
Thats the error:
{"statusCode":500,"message":"org.codehaus.jackson.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value\n at [Source: com.atlassian.confluence.plugins.restapi.filters.LimitingRequestFilter$1#5dff62ce; line: 1, column: 119]","reason":"Internal Server Error"}
The json values for post that you are forming is incorrectly formed, Why dont you use json_encode(); by declaring all the values inside an array, you can change your code like this:
$curl = curl_init();
$post = array(
"id"=>"65604",
"type"=>"page",
"title"=>"page",
"space"=>["key"=>"***"],
"body" =>["storage"=>["value"=>"<p>Here comes the other variable: ".$product_response." </p>", "representation"=>"storage"]],
"version"=>["number"=>11]
);
curl_setopt_array($curl, array(
CURLOPT_PORT => "6003",
CURLOPT_URL => "http://localhost:6003/rest/api/content/65604",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => json_encode($post),
CURLOPT_COOKIE => "JSESSIONID=3A16CBFE8B99E619D62BD4CD6573F184",
CURLOPT_HTTPHEADER => array(
"authorization: Basic xyYS123_test_test-45Sdasds==",
"content-type: application/json",
'Accept: application/json'
),
));

PHP CURL API token retrieval

I've read everything on multiple boards and not succeeded...need the brains trust.
what i'm getting:
HTTP/1.1 400 Bad Request
preload {"error":"unsupported_grant_type"}
my goal: to retrieve the OAuth token from the FQDN/api/token endpoint
i have: endpoint, username & password (no secret or key)
i can curl from the shell and get the token - i just can't get php to do it >/
my code isn't mine anymore since i was pretty sure i was the problem but here is what i'm presently attempting to utilise: (can someone point out where i'm being a muppet!)
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://FQDN/api/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"grant_type\":\"password\",\"username\": \"user#domain.com.au\",\"password\": \"userPasswd\"}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Content-Length: 96",
"Accept: application/json"
),
CURLOPT_HEADER => true,
CURLOPT_POST => true,
CURLOPT_HTTPAUTH => basic
));
$response = curl_exec($curl);
print_r(curl_getinfo($curl));
print_r($response);
curl_close($curl);
According to the comments, you need to send key value pairs:
$data = array(
"grant_type" => "password",
"username" => "username",
"password" => "password"
)
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));

Categories