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!
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');
This is what I am trying
$url = "https://aon-test.yui.com/api/".$this->_token."/".$action;
$headers = array(
'Content-Type:application/xml' ,
'Authorization: Basic '. base64_encode("s!YIYIYIYIUYIY:X")
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 30000);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($xml));
$content = curl_exec($ch);
curl_close($ch);
I am getting 500 - Internal server error.
What mistake I am doing here?
Hey guys im trying to print one api using curl. but so far i wasnt able to get it to work. maybe you guys can help me. the website uses api user and pass both can be viewed in the code i made so far. what this does is it gets the $original_url and gives us a preview based on the template_id which is 20016. you can read the documentation here https://support.dudamobile.com/API/API-Use-Cases/Multiscreen-White-Label-Setup
$original_url = "http://rsportugal.org/index.html";
$data = array("template_id"=>"20016","url"=>$original_url);
$data = json_encode($data);
define("API_USER","...");
define("API_PASS","...");
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.dudamobile.com/api/sites/multiscreen/templates');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$output = curl_exec($ch);
if(curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
$output = json_decode($output);
curl_close($ch);
return $output->site_name;
hopefully you guys can help me
It seems you want to do a http POST on an url that doesn't support POST.
If you want to get 20016 template data, you have to do so :
$templateId = 20016;
define("API_USER","...");
define("API_PASS","...");
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.dudamobile.com/api/sites/multiscreen/templates/' . $templateId);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, API_USER.':'.API_PASS);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$output = curl_exec($ch);
if(curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}
$output = json_decode($output);
print_r($output);
curl_close($ch);
Full API documentation here => Duda mobile api Doc
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))
);