curl commandline to php - php

I have a command i've been having a problem converting to php.
curl -i -X POST -u user:userpass --url http://cloud.netelignet.ca/virtual_machines/:virtual_machine_id/startup.xml

I think it equates to the following:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://cloud.netelignet.ca/virtual_machines/:virtual_machine_id/startup.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$username = 'user'; $password = 'userpass';
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HEADER, 1);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

Related

php curl ubuntu 400 bad request

i have a working curl script and was testing it at my windows pc with xampp
<?php
$username = '123';
$password = '123456';
$url = 'https://192.168.0.100/test';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
$exec = curl_exec($ch);
curl_close($ch);
echo $exec;
?>
Now I want to transfer it to my raspberry pi but i get an 400 error.
Whats the difference with the code ?
Can you help me ?
Thanks

Send HTTP post request with http basic auth in PHP

I have the following post api request, the example provided was using cURL command:
curl "https://muserver.com/api/gettoken" --request POST --include -
-header "Content-Type: application/json" --user test:test
I am trying to perform this request using cURL in PHP but getting authentication error.
following is what I have tried:
ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
I think I should pass the user and pass. in the form of
--user test:test
but really don't know how to do it.
You seem to be lacking the $username and $password variables :
$username='test';
$password='test';
ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
<?php
$url_simple_auth = 'https://www.some_url_need_basic_auth_first.com';
$url_download = 'https://www.some_url_need_basic_auth_first.com/dowonload_file.zip';
$username = 'some_login';
$password = 'some_pass';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_simple_auth);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// login simple auth
$simple_auth = curl_exec($ch);
// download file
curl_setopt($ch, CURLOPT_REFERER, $url_simple_auth);
curl_setopt($ch, CURLOPT_URL, $url_download);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "email=$username&key=$password");
$result = curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($status_code);
var_dump($result);

Calling elasticsearch via curl in php

I have some problems getting data from elasticsearch. I have tested the following simple php script (no framework necessary):
<?php
$url = "curl -XGET http://<myelasticurl>:80/_cat/indices";
$username = "testUser";
$password = "secret";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
var_dump($output);
?>
The only response I get is: "bool(false)"

Confused about command line cURL to PHP script call

I am trying to convert this cURL command line call to a PHP script. I've read many articles on it , and have tried several options but none were able to return a result. Here's the command line call:
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp
:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" \
-d "grant_type=client_credentials"
Here is what I have tried so far:
$data = "grant_type=client_credentials";
$url = "https://api.sandbox.paypal.com/v1/oauth2/token";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch,CURLOPT_HTTPHEADER, array("Accept:application/json","Accept- Language:en_US"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "[$idclient]:[$sekret]");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
var_dump($result);
Please help me in converting it to a PHP script call.
thank you
$url = 'https://api.sandbox.paypal.com/v1/oauth2/token';
$header_array = array(
'Accept: application/json',
'Accept-Language: en_US',
);
$username = 'EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp';
$password = 'EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp';
$data_string = 'grant_type=client_credentials';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);

Curl Post with multi params

I need to do Post curl in php for this command :
curl -k -X POST https://openshift.redhat.com/broker/rest/domains/did/applications/test/events
--user "[UserName]:[Password]" --data "event=restart""
that what i try but it's not working :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://openshift.redhat.com/broker/rest/domains/did/applications/test/events");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "--user "[UserName]:[Password]" --data "event=restart" ");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
I should get the -K and the -X also, so any ideas how to do this ?
See curl_setopt for available options.
<?php
$username = 'username';
$password = 'password';
$url = 'https://example.com/';
$cookie = 'cookie.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'event=restart');
curl_setopt($ch, CURLOPT_POST, true);
$response = curl_exec($ch);
curl_close($ch);
?>

Categories