The documentation says like this:
curl —X POST -c cookies.txt —d "login=demo&password=demo42" https://www.myadcash.com/console/login_proxy.php
Output will be:
{"token":"6333531373034343433623663646836383165693937383167373264323334663"}
My current code
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("login"=>"demo","password"=>"demo42"));
curl_setopt($ch,CURLOPT_URL,"https://www.myadcash.com/console/login_proxy.php");
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . 'cookies.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
No JSON response is showing. Although the text file is saving. Please help me to find right direction. Also if there is any error in the code, please let me know.
-d "login=demo&password=demo42" means data field, thats why pass as post fields not headers.
Therefore these two lines in your curl
curl_setopt($ch, CURLOPT_POST, 1); //Optional
curl_setopt($ch, CURLOPT_POSTFIELDS, "login=demo&password=demo42");
and you must get output.
On the basis of documentation, credentials must be post fields not header that's why no need to put login and password on header.
You don't need
curl_setopt($ch, CURLOPT_HTTPHEADER, array("login"=>"demo","password"=>"demo42"));
Related
I try to send a post request to another php page with the following code:
$vars = ['val1' => 'myval'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
$data = curl_exec($ch);
curl_close($ch);
But on the target page, var_dump($_POST); will output only "1". I had expected something like:
array(val1 = myval) or something similar. So that i can check with isset($_POST["val1"]) if this exists and contains "myval".
Any ideas whats wrong with the request?
Edit:
I have now changed my code to the following:
<?php
$data = array(
'foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
$ch=curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_URL, "target.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
And in target.php:
<?php
echo print_r($_POST, true));
?>
But there is no response. Both files are in the same dir at the server.
That's because curl_setopt() doesn't accept such complext types as array ;) You need to present your data in form of a query string: "postvar1=value1&postvar2=value2&postvar3=value3".
$format = http_build_query($vars);
curl_setopt($ch, CURLOPT_POSTFIELDS, $format);
or
$format = "val1=my-value"; // be careful to create proper encoding
curl_setopt($ch, CURLOPT_POSTFIELDS, $format);
If you're using CURLOPT_POSTFIELDS, you don't need to set CURLOPT_POST as well. In fact, setting it after will result in a potentially incorrect header being sent.
CURLOPT_POSTFIELDS accepts an array, and will set the Content-Type header to multipart/form-data if one is provided. Later, setting CURLOPT_POST will overwrite this to application/x-www-form-urlencoded, and means that the PHP script at the other end will expect data encoded as an HTTP query-string. This is why you're having problems.
You can fix this either by encoding $vars correctly before sending (using http_build_query, as in the other answer), or just remove the call to set CURLOPT_POST. I'd recommend the latter.
I think, i got it. When i set as target something like "target.php", data was not send. But when i use a complete URL, like https://myserver.com/target.php, this will work as expected. Sometimes, its to easy... sorry for stealing your time.
I am facing varied issue. I am able to get response in POSTman but getting below error while using PHP code.
You are not authorized to access this resource
code as below:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml','Accept: application/xml'));
$data = curl_exec($ch);
curl_close($ch);
Unfortunately, different cURL versions behave slightly different and so there is not one valid answer but several approaches that work for different cURL versions.
Here are two suggestions:
From Problems with username or pass with colon when setting CURLOPT_USERPWD
Try adding curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);, or instead CURLAUTH_BASIC.
Something that should always work:
If it won't help, add username and password directly into url like https://user:pass#host.com/path.
You shouldnt turn off certificate verification, instead, get a valid cert, they are for free using letsencrypt.
<?php
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . base64_encode($password)); //here is the change
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml','Accept: application/xml'));
$data = curl_exec($ch);
curl_close($ch);
This is a really longshot and i know that but i have seen quite a few API's that work like that and since the OP seems not to have the documentation of the API i will post this as an answer in case it helps him solve his issue.
If above does not work try to base64_encode($username) as well
I am submitting login form data using curl in php, and from what I've observed the login POST has no response - the lander page is loaded by a subsequent GET request. What I'd like to know is how I can make a POST using curl and then a GET? For reference, here's my current code:
$ch = curl_init('https://<my url>');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: text/html',
));
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'my post fields');
$result = curl_exec($ch);
// I need to make a get request to get the loggedin page here!?
curl_setopt($ch, CURLOPT_URL, 'https:<another_url>');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 0);
$result = curl_exec($ch);
echo curl_errno($ch);
I'm currently getting an error 52 from my POST which is no response, which I think should be expected. The issue is, when I tried to make a GET, I also got errno 52. Also for a sanity check, I retrieved my post fields my grabbing the form data from the network data from chrome dev tools.
Thanks in advance for the help!
First off, iam quite new with both PHP and cURL
From the command:
curl -i 'http://xxx:xxx/v2.0/tokens' -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"auth": {"tenantName": "xxx", "passwordCredentials": {"username": "admin", "password": "xxx"}}}'
I want to produce the results of this command using POST/GET and PHP. I have an apache server and working url.
I just dont know how to make a working code that takes the code and produce the output to an empty page using PHP.
I have been searching examples and I know you got to make use of some of the following:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
?>
I just dont know how to attach the proper flags from the cURL command to the PHP code, probably missing a few vital things aswell to wrap the code up.
Thanks in advance and sorry if I could not explain the my problem good enough.
** ***EDIT:* ****
So I made a code, that works for my needs, it produces a raw result from the cURL command on a PHP page.
Here is the code:
<?php
$request_body = '{"auth": {"tenantName": "xxx", "passwordCredentials": {"username": "admin", "password": "xxx"}}}';
$ch = curl_init('http://xxx:xxx/v2.0/tokens');
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$response = curl_exec($ch);
var_dump($response);
?>
Question now if I can manipulate the output to display in a better fashion, right now its just a big string, some form of JSON pretty print would be amazing.
Read more about curl exec (http://www.php.net/manual/en/function.curl-exec.php)
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
print $data;
?>
The data is returned from the curl_exec command.
Whilst on a different subject, perhaps Uploading files to Zoho API with PHP Curl, a question I posted here recently which contains a Curl class example will help you.
In addition, I suggest you read the manual pages for the PHP curl methods e.g. the curl_exec page - the comments sections often include implementation suggestions.
I try to make a post request with php and curl. Here is my code
//PHP 5.3.5 and curl: 7.18.2
$ch = curl_init();
if(!empty($save_cookie)){
curl_setopt($ch, CURLOPT_COOKIEJAR, $save_cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $save_cookie);
}else{
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, 'http://localhost/post.php');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pars);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_NOBODY, !$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
if (curl_errno($ch)) {
return false;
}
curl_close($ch);
return $postResult;
In http://localhost/post.php, I write
print_r($_SERVER);
The result return of curl is always
[REQUEST_METHOD] => GET
Remove the CURLOPT_NOBODY option and it will work. Or place it above the CURLOPT_POST line.
I think I have encountered this once, when trying to get just the header of a response. Setting
curl_setopt($ch, CURLOPT_NOBODY, true);
effectively instructs curl to issue a HEAD request, which is not a POST request. I think there is no way to just get the header from a POST (and just drop the connection after receiving the header). As a side effect, setting CURLOPT_NOBODY to false sets the request type to GET...
Do you really need the CURLOPT_NOBODY flag?
Try to move the
curl_setopt($ch, CURLOPT_NOBODY, !$body);
line right before the
curl_setopt($ch, CURLOPT_POSTFIELDS, $pars);
line.
There's an interesting post at the curl/set_opt page, shedding some light on this behaviour:
If your POST data seems to be disappearing (POST data empty, request
is being handled by the server as a GET), try rearranging the order of
CURLOPT_POSTFIELDS setting with CURLOPT_NOBODY. CURLOPT_POSTFIELDS has
to come AFTER CURLOPT_NOBODY setting because if it comes after it
wipes out the header that tells your URL target that the
request is a POST not a GET.