Creating User on ownCloud using php curl http post - php

There is all the relevant information present in broken form in the following links on owncloud related websites and from stackoverflow itself:
User Provisioning Api - Owncloud
PHP + curl, HTTP POST sample code
Create user on ownCloud using Ajax Jquery
User Provisioning - php Authentication error
I am trying to do something very simple :
I have setup an owncloud server in my localhost,
I have an html page that takes in string values of user name and password
I send the page request to be processed by the following php script.
<?php
echo "Begun processing credentials , first it will be stored in local variables" . "<br/>";
// Loading into local variables
$userName = $_POST['username'];
$RRpassword = $_POST['password'];
echo "Hello " . $userName . "<br/>";
echo "Your password is " . $RRpassword . "<br/>";
// Add data, to owncloud post array and then Send the http request for creating a new user
$ownCloudPOSTArray = array('username' => $userName, 'password' => $RRpassword );
$url = 'http://localhost/owncloud/ocs/v1.php/cloud/users';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ownCloudPOSTArray);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo "<br/>Created a new user in owncloud";
?>
I get the output like:
Begun processing credentials , first it will be stored in local
variables Hello Frank Your password is frankspassword
failure 997 Unauthorised Created a new user in owncloud
I also tried to login to own cloud using following php script:
// Login As Admin
$ownAdminname = 'ownAdmin';
$ownAdminpassword = 'ownAdminPassword';
$ch = curl_init('http://localhost/owncloud');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$ownAdminname:$ownAdminpassword");
$output = curl_exec($ch);
curl_close($ch);
echo $output;
Even this one fails.
So in short it doesn't work. I am also unable to login via similar script to owncloud. What is the proper way to do this ? What settings am I missing ? Can someone help please ?

Since this question pertains to owncloud specifically, I created an account and posted a question linking this one to it in owncloud forum.
There I was suggested by the owncloud master #RealRancor, the following,
Just had another look, maybe its just easy to replace:
$url = 'http://localhost/owncloud/ocs/v1.php/cloud/users';
with
$url =
'http://adminuser:adminpass#localhost/owncloud/ocs/v1.php/cloud/users';
as shown in the documentation.
And amazingly it worked like a charm. So here is the entire modified php script:
<pre>
<?php
echo "Begun processing credentials , first it will be stored in local variables" . "<br/>";
// Loading into local variables
$userName = $_POST['username'];
$RRpassword = $_POST['password'];
echo "Hello " . $userName . "<br/>";
echo "Your password is " . $RRpassword . "<br/>";
// Login Credentials as Admin
$ownAdminname = 'ownAdmin';
$ownAdminpassword = 'ufi2016%%';
// Add data, to owncloud post array and then Send the http request for creating a new user
$url = 'http://' . $ownAdminname . ':' . $ownAdminpassword . '#localhost/owncloud/ocs/v1.php/cloud/users';
echo "Created URL is " . $url . "<br/>";
$ownCloudPOSTArray = array('userid' => $userName, 'password' => $RRpassword );
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ownCloudPOSTArray);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo "Response from curl :" . $response;
echo "<br/>Created a new user in owncloud<br/>";
// Add to a group called 'Users'
$groupUrl = $url . '/' . $userName . '/' . 'groups';
echo "Created groups URL is " . $groupUrl . "<br/>";
$ownCloudPOSTArrayGroup = array('groupid' => 'Users');
$ch = curl_init($groupUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ownCloudPOSTArrayGroup);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo "Response from curl :" . $response;
echo "<br/>Added the new user to default group in owncloud";
?>
</pre>
And here is the output:
Begun processing credentials , first it will be stored in local variables
Hello Frank
Your password is frankspassword
Created URL is http://ownAdmin:ufi2016%%#localhost/owncloud/ocs/v1.php/cloud/users
Response from curl : ok 100
Created a new user in owncloud
Created groups URL is http://ownAdmin:ufi2016%%#localhost/owncloud/ocs/v1.php/cloud/users/Frank/groups
Response from curl : ok 100
Added the new user to default group in owncloud

The owncloud documentation states that authentication is done by means of a basic HTTP Authentication header. What you are currently doing is sending the credentials as parameters to the API call. You need to add the following line:
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $RRpassword);
There's also a typo in CURLOPT_RETURNTRANSFER ($curl instead of $ch).

As your linked post (Create user on Owncloud) shows you need to an basic auth header with some credentials. You need to provide the admin credentials, afaict.

Related

Can i store static API Key in php file safely

i want to use FCM in my android application , and i was wondering what is the best practice to store a static API such as Server Key API of FCM .
And after a lot o reading i found that even using ProGard doesn't save the API securely, so the conclusion is to never store keys in the app source code .
My solution is to save the key in a .php file like this, so that every time a device send a notification it sends data ( Notification title , message .. ) to my server that contain the server key and redirect the request to https://fcm.googleapis.com/fcm/send
My php file :
<?php
require_once __DIR__ . '/notification.php';
$notification = new Notification();
$title = "Notification Title";
$message = "Notification Message";
$notification->setTitle($title);
$notification->setMessage($message);
$firebase_api = "API_STATIC_SERVER_CODE_THAT_I_NEED_TO_HIDE";
$requestData = $notification->getNotificatin();
$fields = array(
'to' => '/topics/Test_Topic',
'data' => $requestData,
);
$url = 'https://fcm.googleapis.com/fcm/send';
$headers = array(
'Authorization: key=' . $firebase_api,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarily
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if($result === FALSE){
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo '<h2>Result</h2><hr/><h3>Request </h3><p><pre>';
echo json_encode($fields,JSON_PRETTY_PRINT);
echo '</pre></p><h3>Response </h3><p><pre>';
echo $result;
echo '</pre></p>';
?>
But, is that safe ? Could hackers some how download my php content ( without hacking my server ) , or the fact that i am using HTTPS connections keeps my data safe .
And what else can i do if it's not safe enough
Thank you

Generate Pinterest access token dynamically with PHP API

I developed a Pinterest plugin for WordPress users. Now, I am facing token generation problem with Pinterest API. I have set and configure accordingly to API docs. In Step 1 you get an access code and in Step 2, you need to request for access token with the help of access code. Step 1 going to succeed but in Step 2 throwing error response.
I already asked to Pinterest Support through email but they are saying:
We're a small team right now and we can't offer development support or
consult for the API just yet. For more support around using our API,
we encourage you to use developer resources like Stack Overflow.
The code I did:
I already made an APP and used the give App ID and Secret, and redirect URL also set in APP same below:
$client_id = "&client_id=496200555XXXXXXXX1778834";
$client_secret = "&client_secret=48d62d7c21aa432bb5320c0aeXXXXXXXXXXX75933f6295db1bae61ffa66ca31";
$authorization_url = "https://api.pinterest.com/oauth/?";
$response_type = "response_type=code";
$state = "&state=weblizar_app";
$scope = "&scope=read_public,read_relationships";
$redirect_uri = "&redirect_uri=https://weblizar.com/pinterest-access-token.php";
$access_token_url = "https://api.pinterest.com/v1/oauth/token?";
$grant_type = "grant_type=authorization_code";
// Step 1: get authorization code
$access_code_url = $authorization_url . $response_type . $redirect_uri . $client_id . $scope . $state;
echo "<a href=$access_code_url>Get Authorization Code</a>";
// Step 2: exchange the access token
if(isset($_GET['code'])) {
$authorization_code = $_GET['code'];
$access_code_url = $access_token_url . $grant_type . $client_id . $client_secret ."&code=" .$authorization_code; echo "<br>";
echo "Curl post URL - "; echo "<br>";
$ch = curl_init();
echo $url = $access_code_url;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = trim(curl_exec($ch));
curl_close($ch);
echo "Curl Post Response"; echo "<br>";
echo "<pre>";
print_r($result);
echo "<pre>";
}
Response Returned After Step 2
{
"message": "405: Method Not Allowed",
"type": "http"
}
As everyone knows Pinterest token generation URL no more available for users or closed down.
You can test, the code is live on site: https://weblizar.com/pinterest-access-token.php
Any kind of help really appreciated. Thanks in advance.

php curl get json and decode then display, fails displays nothing

hello my teacher has given me this weeks work and i need to get my json data from a url and display it as php using json_decode, the php json file which i access with the url displays the songs as an json array and works fine, and i copied my teachers curl example to access the url, but it fails to display anything what have i done wrong ?
Edit: i do need to enter a password and username to access the url so i changed the code as told, but still nothing displays, i changed the password and username for security but the format and length are the same
<?php
$username = "2foobar90";
$password = "123456";
// Initialise the cURL connection
$connection = curl_init();
curl_setopt($connection, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($connection, CURLOPT_USERPWD, $username . ":" . $password);
// Specify the URL to connect to - DOUBLE CLICK link to test
curl_setopt($connection, CURLOPT_URL, "https://edward2.solent.ac.uk/~martine/year3/hits.php?artist=David+Bowie");
// This option ensures that the HTTP response is *returned* from curl_exec(),
// (see below) rather than being output to screen.
curl_setopt($connection,CURLOPT_RETURNTRANSFER,1);
// Do not include the HTTP header in the response.
curl_setopt($connection,CURLOPT_HEADER, 0);
// Actually connect to the remote URL. The response is
// returned from curl_exec() and placed in $response.
$response = curl_exec($connection);
// Close the connection.
curl_close($connection);
$data = json_decode($response, true);
for($i=0; $i<count($data); $i++)
{
echo "Song id " . $data[$i]["songid"] . " " .
"Title " . $data[$i]["title"] . " " .
"Artist " . $data[$i]["artist"] . " " .
"Chart position " . $data[$i]["chart"] . "<br/>";
}
?>
The link requires basic authentication. Your browser remembers your username/password when you click the link. You can see request headers with chrome inspector.
Click link
Open your browser inspector
Click Network section
Find document
You can see Authorization data under the Request Headers
If you want to use authorization with curl add this:
curl_setopt($connection, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($connection, CURLOPT_USERPWD, $username . ":" . $password);

Does Marketo API block curl on a per account basis?

I am trying to connect to the Marketo.com REST API using curl.
I can't get a response from the identity service. I only get an error message
"[curl] 6: Couldn't resolve host 'MY_CLIENT_ENDPOINT.mktorest.com'
,
but I can print the constructed url and paste it into a browser address bar and this will provide the expected response with the access_token element.
I can use curl in php and in a terminal to access my gmail account so curl is able to access an https service.
I have tried sending the parameters in the curl url as a get request and also by declaring them with curl's -F option as a post request
My application uses dchesterton/marketo-rest-api available on github, but I have also tried a simple php curl request just to get the access token.
private function getToken() {
$url = "$this->client_url/identity/oauth/token?grant_type=client_credentials&client_id=$this->client_id&client_secret=$this->client_secret";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$errors = curl_error($ch);
curl_close($ch);
file_put_contents($this->logDir . 'access_token_response' . date('Y-m-d') . '.txt', $url . "\n" . $response . "\n", FILE_APPEND);
if ($errors) {
file_put_contents($this->logDir . 'access_token_errors' . date('Y-m-d') . '.txt', $errors . "\n", FILE_APPEND);
}
return $response['access_token'];
}
Again, this fails with the same error but produces a perfectly formed url that I can paste into the browser and get a valid response.
I have also tried this using post instead of get as I have for every other test mentioned, and these have been tried on my localhost and on a test server.
Can anyone explain to me why this would fail?
Does Marketo block curl on a per account basis?
I was trying to implement something similar but my code wasn't working. I'm not sure exactly what is failing but I tried your code and it seems to work perfectly after some slight modifications:
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($request_data));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($curl);
$errors = curl_error($curl);
curl_close($curl);
I hope this helps.

rage4 API, create a basic domain = null

I am using the sample code from -> https://gbshouse.uservoice.com/knowledgebase/articles/109834-rage4-dns-developers-api#create_regular_domain to create a basic domain in the Rage4 system for the very first time via API.
But following the call I get a NULL value returned.
I double checked my username and pass/api key and it is verified good.
I don't get any log errors in the rage4 system though.
code used:
<?php
$name = 'testdom4.com';
$email = 'admin#testdom4.com';
$username = 'user#testdom4.com';
$password = '4444444444444444444a1e42a';
$ch = curl_init("https://secure.rage4.com/RAPI/CreateRegularDomain/?name=" . $name . "&email=" . $email);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
$result = curl_exec($ch);
var_dump(json_decode($result, true));
?>
Did I miss something in the backend to initialize?
How do I create a basic domain via api in the rage4 system?

Categories