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
Related
I'm trying to make a request behind a proxy to stripe api using php and curl. The following works if I'm not behind a proxy:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/balance");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$a_pass");
$out = curl_exec($ch); # returns my balance
curl_close($ch);
A request to any other domain using the proxy works:
$username = 'user';
$password = 'pass';
$proxy = 'proxy:port';
$ch = curl_init('https://api64.ipify.org?format=json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$username:$password");
$result = curl_exec($ch); # returns the proxy ip
curl_close($ch);
But if I mix both, It doesn't work and a 403 error is returned by stripe api:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/balance");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$a_pass");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$username:$password");
$out = curl_exec($ch);
if (curl_errno($ch)) {
print_r($curl_error($ch)); # 403
}
curl_close($ch);
It seems the CURLOPT_USERPWD is lost if the request is made using a proxy.
Any idea how to use a CURLOPT_PROXYUSERPWD, CURLOPT_PROXY and CURLOPT_USERPWD sucessfully on the same request?
Notes:
I cannot install any additional software on this machine.
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)"
I have a web server secured by http. I want to log in to this server1 via another one. When i use this url : http://username:password#server1. It works.
Now i want to execute this url from another webpage in server2. I'm using Curl.
<?php
$username='username';
$password='password';
$URL='http://server1';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
$result=curl_exec ($ch);
echo $result ;
curl_close ($ch);
?>
This is not working for me. I have just the pop-up to log with http. What's wrong in my php page ?
Try,
$username ='username';
$password ='password';
$yourEfforts = array();
$yourEfforts['username']=$username; //stored in yourEfforts array.
$yourEfforts['password']=$password; //stored in yourEfforts array.
$URL='http://server1';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, $yourEfforts);//Your username and password
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //get status code
echo "<pre>";
var_dump(curl_exec($ch));//debug curl exec output
var_dump(curl_getinfo($ch));//debug curl info
var_dump(curl_error($ch));//debug curl error
$result=curl_exec ($ch);
echo $result ;
curl_close ($ch);
Hope it helps!
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);
I am trying to fetch a json string from a password protected url.
I think my code should work but when I open my script in the browser I just have a blank page.
Here's my code:
<?php
error_reporting(E_ALL);
$url = 'https://api.domain.com?api=latest&format=json';
$username = 'username';
$password = 'password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
//curl_setopt($ch, CURLOPT_REFERER, $url);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$data = json_decode($result, true);
print_r($data);
?>
Any help would be much appreciated.
Try adding this
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);