I am trying to edit an existing Basecamp project via the new Basecamp Api. I am receiving this error:
lexical error: malformed number, a digit is required after the minus sign. --------------- ---------------6 (right here) ------^
My Code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_URL, 'https://basecamp.com/****/api/v1/projects/****.json');
curl_setopt($ch, CURLOPT_USERAGENT, "User-Agent : Holy Grail (user#example.com)");
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, array("name" => "from cURL"));
$result = curl_exec($ch);
echo $result;
curl_close($ch);
if ($result == false) {
echo "Fetch failed" ;
}
else {
$obj = json_decode($result, true);
}
//var_dump($obj);
?>
I'm sure I'm just doing something stupid, but any help is appreciated.
Thanks!
UPDATE
What I have now:
$username = 'user';
$password = 'pass';
$data = json_encode(array("name" => "from cURL"));
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_URL, 'https://basecamp.com/****/api/v1/projects/*****.json');
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'User-Agent : Holy Grail (user#example.com)');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type :application/json',
'Content-Length: ' .strlen($data)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
echo $result;
curl_close($ch);
if ($result == false) {
echo "Fetch failed" ;
}
else {
$obj = json_decode($result, true);
}
//var_dump($obj);
?>
</body>
</html>
BasecampAPI accepts only JSON data, you can see here in -d parameter -
curl -u username:password \
-H 'Content-Type: application/json' \
-H 'User-Agent: MyApp (yourname#example.com)' \
-d '{ "name": "My new project!" }' \
https://basecamp.com/999999999/api/v1/projects.json
So you're not sending JSON data in this line -
curl_setopt($ch, CURLOPT_POSTFIELDS, array("name" => "from cURL"));
Remove the CUSTOMREQUEST option and add CURLOPT_PUT. Modify your code to -
$data_string = json_encode(array("name" => "from cURL"));
...
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_PUT, True);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
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);
}
I followed the instructions in the official vsphere site to get info from the server and from the answer of another user here .
From what I have understood, firstly I have to get the session id (cis id), but I got "null" as a result.
I tried multiple codes but the result is the same:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = array(
'Content-Type: application/json',
'Accept: application/json',
'vmware-use-header-authn: test'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'administrator#vs.dom:userpassword');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
echo 'Curl Error: ' . curl_error($ch);
exit;
}
$sid = $out;
dd($out);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/vcenter/vm");
$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);
curl_close($ch);
The result is null.
this is a script word 100% using PHP
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'username' . ":" . 'password');
$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
echo 'Curl Error: ' . curl_error($ch);
exit;
}
$sid = $out->value;
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/vcenter/vm");
$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);
curl_close($ch);
?>
when you debug your code, the $out variable return null?
If yes, can you check if your curl returns 56 - OpenSSL SSL_read: Success error in $out = json_decode(curl_exec($ch));?
This error occoured with my code, and I see wich cURL in 7.67 version cause this trouble.
My solution was to downgrade cURL version to 7.65.
It works for me!
I had the same problem, and eventually understood that it is caused by this header:
curl_setopt($ch, CURLOPT_POST, true);
And replacing it with this one solves the issue:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
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.
I'm experimenting the Basecamp API by trying to create a project.
I'm using the following code:
#CONTENT-TYPE HEADER
$helloHeader = "User-Agent: $appName ($appContact)";
#URL
$url= $baseUrl.'/projects.json';
#JSON DATA
$data = array
(
'nome' => 'project xpto'
);
$data_aux = json_encode($data);
$data_string = 'json=' . $data_aux . '&';
#INITIALIZE
$ch = curl_init($url);
#standard
curl_setopt($ch, CURLOPT_USERPWD, $credentials);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
#OBTAIN DATA
//curl_setopt($ch, CURLOPT_HTTPGET, true);
#SEND DATA
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"User-Agent: " . $appName . "(" . $appContact . ")")
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$response = curl_exec($ch);
#ERRORS?
$errno = curl_errno($ch);
$error = curl_error($ch);
#CLOSE
curl_close($ch);
And I'm having the following error:
lexical error: invalid char in json text.
json={"nome":"project xpto"}&
(right here) ------^
Any idea of what I'm doing wrong? I've tried changing the array by several ways but I can't really understand this error!
I'm trying to set up account creation via a payment form on my website using ZenDesk's API. The example code they give is:
curl -v -u {email_address}:{password} https://{subdomain}.zendesk.com/api/v2/users.json \
-H "Content-Type: application/json" -X POST -d '{"user": {"name": "Roger Wilco", "email": "roge#example.org"}}'
Since I need to include PHP variables, I'm trying to use this:
$data = array("name" => $entry["1"], "email" => $entry["3"], "role" => "end-user");
$data_string = json_encode($data);
$ch = curl_init('https://xxxx.zendesk.com/api/v2/users.json');
curl_setopt($ch, CURLOPT_USERPWD, "xxxx#example.com:xxxx");
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))
);
$result = curl_exec($ch);
However, it's not working. Is my code correct in terms of duplicating the function of the first snippet?
I found another example of ZenDesk's API and was able to come up with this:
<?PHP
define("ZDAPIKEY", "SECRETKEYGOESHERE");
define("ZDUSER", "me#mysite.com");
define("ZDURL", "https://mysite.zendesk.com/api/v2");
/* Note: do not put a trailing slash at the end of v2 */
function curlWrap($url, $json, $action)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_URL, ZDURL.$url);
curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY);
switch($action){
case "POST":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
break;
case "GET":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
break;
case "PUT":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
default:
break;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$decoded = json_decode($output);
return $decoded;
}
$arr = array("z_name"=>$namevariable,
"z_email"=>$emailvariable,
"z_role"=>"end_user",
"z_verified"=>"yes"
);
$create = json_encode(array('user' => array('name' => $arr['z_name'], 'email' => $arr['z_email'], 'role' => $arr['z_role'])), JSON_FORCE_OBJECT);
$data = curlWrap("/users.json", $create, "POST");
var_dump($data);
?>
It appears to be working on its own, so this answers the question as it exists here.
Thanks for your help everyone :)
I know the question is answered, but since I found it while having the same issue and since it did not solve my issue, I figured I'd post what I did. Hopefully, it can help someone else.
Here is the combination that worked for me in a case where I had to submit json data via PUT:
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 90);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Content-Length: ' . strlen($json), 'X-HTTP-Method-Override: PUT'));
Note that it does NOT require CURLOPT_CUSTOMREQUEST or CURLOPT_PUT since the X-HTTP-Method-Override: PUT parameter takes care of that.