I tried to update an existing Nextcloud user through their API. When I do it directly via shell it works
curl -u user:pass -X PUT "https://example.org/ocs/v1.php/cloud/users/admin" -H "OCS-APIRequest: true" -d key="quota" -d value="5GB"
But when I try to do it via PHP with the following code it always returns "failure 997"
$url = 'https://' . $ownAdminname . ':' . $ownAdminpassword . '#example.org/ocs/v1.php/cloud/users/admin';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$fields = array("quota" => "5GB");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'OCS-APIRequest: true'
));
$response = curl_exec($ch);
curl_close($ch);
echo "Response: ".$response;
The difference between the cURL command and the PHP code you pasted lies in a poorly designed user provisioning API.
Using these cURL arguments:
-d key="quota" -d value="5GB"
... is not equivalent to the fields you're posting:
$fields = array("quota" => "5GB");
... but rather:
$fields = array(
'key' => 'quota',
'value' => '5GB',
);
The explanation for the 997 code you're getting can be found in https://github.com/owncloud/core/blob/v10.0.3/apps/provisioning_api/lib/Users.php#L269-L272: since there's no "key" key in the data submitted ($parameters['_put']['key'] will evaluate as null) and hence the error.
Related
I have a curl command that worls perfectly form the SSH shell, but cannot get it working with PHP Version 7.4.3.
I have tried several times using just about every example I can find on the Internet, all of them return a blank html page when run.
Here is the curl command (with user/pass and IP etc changed for obvious reasons);
curl \
--user someuser:password \
--data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getinfo", "params": [] }' \
-H 'content-type: text/plain;' \
http://195.86.111.23:8223/
Can someone please educate me on how to turn this into something PHP can use?
I am currently testing with this code, but get an error "{"result":null,"error":{"code":-32600,"message":"Params must be an array"},"id":"curltest"} "
$payload=array();
$payload['jsonrpc']='1.0';
$payload['id']='curltest';
$payload['method']='getinfo';
$payload['params']='[]';
$payload=json_encode($payload);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, $user . ":" . $pass);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res=curl_exec($ch);
echo $res;
I have also tried using an automated tool here - https://incarnate.github.io/curl-to-php/ which seems to convert my curl command to PHP okay, but the page is empty on loading the provided code.
$payload = ["jsonrpc" => "1.0", id => "curltest", "method" => "getinfo", "params" => [] ];
Simple solution!
I have the following Curl code, which I used the curl to PHP converter to convert to PHP code
curl https://a.klaviyo.com/api/v1/list/dqQnNW/members \
-X POST \
-d api_key=API_KEY \
-d email=george.washington#example.com \
-d properties='{ "$first_name" : "George", "Birthday" : "02/22/1732" }' \
-d confirm_optin=true
The code I got was
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://a.klaviyo.com/api/v1/list/dqQnNW/members");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "api_key=API_KEY&email=george.washington#example.com&properties='{&confirm_optin=true");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
However this doesn't seem right because of the "properties" line which doesn't get translated into the PHP code.
How do I incorporate the "properties" line with a $first_name variable into my PHP code?
yeah it's definitely not correct, the converter screwed up the CURLOPT_POSTFIELDS line.
try
curl_setopt($ch,CURLOPT_POSTFIELDS,'api_key=API_KEY&email=george.washington#example.com&properties={ "$first_name" : "George", "Birthday" : "02/22/1732" }&confirm_optin=true');
instead. and if you can be arsed, send a bugreport to the author of the converter, this is definitely a bug.
and protip, in php, you might wanna use the http_build_query function instead for application/x-www-form-urlencoded-encoding, and json_encode for json encodig, eg
curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build_query ( array (
'api_key' => 'API_KEY',
'email' => 'george.washington#example.com',
'properties' => json_encode ( array (
'$first_name' => 'George',
'Birthday' => '02/22/1732'
) ),
'confirm_option' => true
) ) );
also note, are you sure you're using this api correctly in your curl example? it's mixing application/x-www-form-urlencoded-encoding with json-encoding, that's unusual. a weird design choice if correct. i'd double-check if that's the correct behavior with the docs if i were you.
I'm trying to get a response using curl according to this doc. But I'm new to cURL/REST and don't have an idea how to do that.
For an example there is this code in the doc that says it returns a list of folders
curl -X GET -u "email:password" https://www.seedr.cc/rest/folder ,
but when I try it in PHP it doesn't work. how can I do this ->
Edit : it seems this is a command line curl . I tried it with command line and it works, but I need to know how to use this in a PHP script .
You don't need use curl. You can do it directly in php.
$url="https://www.seedr.cc/rest/folder"
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$username:$password")
)
));
$data = file_get_contents($url, false, $context);
This is an example of how to use cURL with php. You can use the curl_setopt function to set and change the options to suit your peculiar case. Some of them are actually optional. You can check the table here to get a full list of what they do
$durl = "https://www.seedr.cc/rest/folder?email=$password" ;
$ch = curl_init() ;
curl_setopt($ch,CURLOPT_URL, $durl);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FAILONERROR, 1);
$dinf = curl_exec ($ch);
if(!curl_errno($ch) ){
echo $dinf ;
}else{
echo curl_error($ch) ;
}
When calling an API endpoint in PHP, what is the difference between passing as a parameter in the URL using
$str = '{\'since\':\'2015-01-01\',\'until\':\'2015-01-22\'}';
'blahblah?access_token=xxx&time_range=' . $str;
versus
$str = '{\'since\':\'2015-01-01\',\'until\':\'2015-01-22\'}';
'blahblah?access_token=xxx&time_range=' . urlencode($str);
Apart from the latter making more likely the URL becoming 'too long'? Knowing that no one is going to see the URL, I just want to retrieve data from the API and output to CSV
Many thanks
via something like this:
$data = array("name" => "Hagrid", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://api.local/rest/users');
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);
instead of passing this information in the url via a GET request?
Edit: Actually, can someone just tell me where I can read up on curl command line flags like -G, or how to implement the below in PHP? Thanks
curl -G \
-d "fields=['impressions','campaign_group_name','cost_per_action_type']" \
-d "action_breakdowns=['action_type','action_carousel_card_name']" \
-d "level=adgroup" \
-d "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/<API_VERSION>/act_<AD_ACCOUNT_ID>/insights"
How i can run this commando curl:
curl -F "param01=value01" -F "param02=value02" -v http://example.com/Home/Login
Using PHP ?
Doubt is because the parameter -F, I never used it...
UPDATE:
curl man page:
-F, --form
(HTTP) This lets curl emulate a filled-in form in which a user has
pressed the submit button. This causes curl to POST data using the
Content-Type multipart/form-data according to RFC 2388. This enables
uploading of binary files etc. To force the 'content' part to be a
file, prefix the file name with an # sign. To just get the content
part from a file, prefix the file name with the symbol <. The
difference between # and < is then that # makes a file get attached in
the post as a file upload, while the < makes a text field and just get
the contents for that text field from a file.
You'll use the POST example, at the bottom of my response.
If param01 and param02 are GET/url parameters, this will work.
<?php
// Setup our curl handler
if (!$ch = curl_init())
{
die("cURL package not installed in PHP");
}
$value1 = urlencode("something");
$value2 = urlencode("something");
curl_setopt($ch, CURLOPT_URL,'http://example.com/Home/Login?param01='.$value1.'¶m02='.$value2);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE); // TRUE if we want to track the request string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // TRUE to return the transfer as a string
$response = curl_exec($ch);
if(curl_error($ch) != "")
{
die("Error with cURL installation: " . curl_error($ch));
}
else
{
// Do something with the response
echo $response;
}
curl_close($ch);
If they are POST (form data):
<?php
// Setup our curl handler
if (!$ch = curl_init())
{
die("cURL package not installed in PHP");
}
$value1 = urlencode("something");
$value2 = urlencode("something");
$data = array(
'param1' => $value1,
'param1' => $value2,
)
curl_setopt($ch, CURLOPT_URL,'http://example.com/Home/Login');
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE); // TRUE if we want to track the request string
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // TRUE to return the transfer as a string
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
if(curl_error($ch) != "")
{
die("Error with cURL installation: " . curl_error($ch));
}
else
{
// Do something with the response
echo $response;
}
curl_close($ch);
Everything you need and more:
http://php.net/curl
simple example:
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "http://www.example.com/yourscript.php",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'field1' => 'some date',
'field2' => 'some other data',
),
);
curl_setopt_array($ch, $curlConfig)
$result = curl_exec($ch);
curl_close($ch);