Related
This works from the command line how do I convert this to PHP I have tried anything but nothing seems to work.
curl -H "Authorization: Bearer APIKEY" https://www.gocanvas.com/apiv2/forms.xml -F 'username=XXXXXXX#XXXXXXXXXX.com'
Here is a function I have tried.
function getgocan(){
$url = 'https://www.gocanvas.com/apiv2/forms.xml?username=XXXXX#XXXXXXXXXX.com';
$ch = curl_init();
$jsonData = array(
'text' => ''
);
$jsonDataEncoded = json_encode($jsonData, true);
$header = array();
$header[] = "APIKEY";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
}
-F sends form fields in the POST data, not URL query parameters. So you need to put username=XXX into CURLOPT_POSTFIELDS, and it shouldn't be JSON.
You can give an associative array to CURLOPT_POSTFIELDS, and it will encode it automatically.
function getgocan(){
$url = 'https://www.gocanvas.com/apiv2/forms.xml';
$ch = curl_init();
$params = array(
'username' => 'username=XXXXX#XXXXXXXXXX.com'
);
$header = array(
'Authorization: Bearer APIKEY'
);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
curl_close($ch);
var_dump($$result);
}
Hello i want to convert this command line in php curl
curl -X POST https://api.imgur.com/3/upload -H "Authorization: Client-ID cdeebfbfe0d825a" -F "image=http://IP/output/1530096054.png"
MY PHP code
$URL = 'https://api.imgur.com/3/upload';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
curl_close ($ch);
print_r($ch);
print_r($status_code);
Thanx for help
I found the solution
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/3/upload');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'image=http://IP/output/1530096054.png');
$headers = array();
$headers[] = 'Authorization: Client-ID 46as4d6as54d6as46';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
I'm trying now to execute PUT request using cURL PHP.
Unfortunately, I receive an error "HTTP Status 415 - Unsupported Media Type".
Here is my code :
$ch = curl_init();
$proxy = 'api.test.sandbox.mobile.de';
$proxy_port = "8080";
$loginpassw = 'XXX:YYY';
$url='https://services.mobile.de/seller-api/sellers/1086/ads/509939';
$headers = array();
$headers[] = "Content-type: application/json";
$data = array(
'mileage' => '10000',
);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $loginpassw);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
echo $data;
curl_close($ch);
What is wrong with py request ?
According to their API, you need to send data in either JSON or XML format and specify Content-Type header
https://services.mobile.de/docs/seller-api.html#_media_types
So set the header for writing:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/vnd.de.mobile.api+json');
And change POSTFIELDS to
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
I'm trying to develop a PHP client to interact with HBase REST API. But I can't find the way to create scanner using JSON request. All examples I could find are using XML format. Just wonder if it's possible to send using JSON format.
The code below returned error message:
"HTTP/1.1 500 Can not deserialize instance of java.lang.String out of START_ARRAY token"
<?php
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);
if (!file_exists(__DIR__ . '/tmp')) {
mkdir(__DIR__ . '/tmp', 0777);
}
$scannerFile = __DIR__ . '/tmp/scanner';
if (!file_exists($scannerFile)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
curl_setopt($ch, CURLOPT_USERPWD, ":");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
'batch' => 10,
'filter' => array(
array(
'type' => 'PrefixFilter',
'value' => 'u123',
),
),
)));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);
file_put_contents($scannerFile, $page);
curl_close($ch);
} else {
$s = file_get_contents($scannerFile);
$parts = explode('Location: http://hbase_uri:20550/news/scanner/', $s);
$parts = explode("\n", $parts[1]);
$parts[0] = trim($parts[0]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner/" . $parts[0]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
curl_setopt($ch, CURLOPT_USERPWD, ":");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);
file_put_contents(__DIR__ . '/tmp/result', $page);
curl_close($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner/" . $parts[0]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
curl_setopt($ch, CURLOPT_USERPWD, ":");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);
file_put_contents(__DIR__ . '/tmp/result_delete', $page);
curl_close($ch);
}
I got the answer by other's hint:
<?php
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
);
if (!file_exists(__DIR__ . '/tmp')) {
mkdir(__DIR__ . '/tmp', 0777);
}
$scannerFile = __DIR__ . '/tmp/scanner';
if (!file_exists($scannerFile)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
curl_setopt($ch, CURLOPT_USERPWD, ":");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
'batch' => 10,
'filter' => json_encode(array(
'type' => 'PrefixFilter',
'value' => 'u123',
)),
)));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);
file_put_contents($scannerFile, $page);
curl_close($ch);
} else {
$s = file_get_contents($scannerFile);
$parts = explode('Location: http://hbase_uri:20550/news/scanner/', $s);
$parts = explode("\n", $parts[1]);
$parts[0] = trim($parts[0]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner/" . $parts[0]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
curl_setopt($ch, CURLOPT_USERPWD, ":");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);
file_put_contents(__DIR__ . '/tmp/result', $page);
curl_close($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://hbase_uri:20550/news/scanner/" . $parts[0]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);
curl_setopt($ch, CURLOPT_USERPWD, ":");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($ch);
file_put_contents(__DIR__ . '/tmp/result_delete', $page);
curl_close($ch);
}
For the future visitors.
Using #kiang answer I have successfully made a CURL POST creating an scanner:
curl \
--verbose \
--request POST \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{"batch":10,"filter":"{\"type\":\"PrefixFilter\",\"value\":\"u123\"}"}' \
'https://hbase:20550/namespace:table/scanner/'
This returned
Location -> Buffer(https://hbase:20550/namespace:table/scanner/151302168977413777789)
...
AhcWSResponse(StandaloneAhcWSResponse(201, Created))
What might be useful:
Using those examples (you need to escape " signs)
Finding the docs of ScannerModel.
Hi all! I have curl command (audio file upload):
curl -k -v -H "Expect: " -H "Content-Type:application/octet-stream" --data-binary '#/Path/To/File/test.wav' -X POST 'https://myapi.com?param1=xxx¶m2=yyy'
File successfully uploaded and readable. But when I used php script:
$filename = 'test.wav';
$file = '#/Path/To/File/test.wav';
$post_url = "https://someapi.com?param1=xxx¶m2=yyy";
$post_str = array("$filename" => $file);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/octet-stream'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$http_body = curl_exec($ch);
var_dump($http_body);
File successfully uploaded and test.wav is not valid (with some error). What am I doing wrong in the script?
I suspect the issue is these lines:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/octet-stream'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
The second call will remove the "Content-Type" Header. Try consolidating these:
$headers = array('Content-Type:application/octet-stream','Expect: ');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$filename = 'test.wav';
$file = '/Path/To/File/test.wav';
$post_url = "https://someapi.com?param1=xxx¶m2=yyy";
$post_str = file_get_contents($file);
$headers = array('Content-Type:application/octet-stream', 'Expect: ');
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_VERBOSE,true);
curl_setopt($ch, CURLOPT_STDERR, fopen("/Path/to/header.txt", "w+"));
$http_body = curl_exec($ch);
It's work perfect. I solved the problem myself. File upload to the server binary. Thank you all!