php- curl isn't working for POST requests - php

I want to send a POST request using curl but don't know why it isn't working. The HTTP request is as:
Type: POST
URL: https://abc.com/server/386594/actions/?do=open
HEADERS: Authorization: Basic 1234abc
When I am make request using curl (CLI) then it works fine but in php I do not get any response. My code is:
$headers = array("Authorization: Basic 1234abc");
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch1, CURLOPT_URL, "https://abc.com/server/386594/actions/");
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CUTLOPT_POSTFIELDS, "do=open");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
$vnc = curl_exec($ch1);
echo $vnc;
echo curl_error($ch1);
Where am I wrong? :)

Please put the code as below....
url is not secure url, so that remove the HTTPS and write simple HTTP.
$headers = array("Authorization:Basic 1234abc");
//remove the space
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$host_url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST,true);
//remove 1 and put 'true' keyword
curl_setopt($ch, CURLOPT_POSTFIELDS,"do=open");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
$curl_response = curl_exec($ch);

Related

PHP CURL GET request return 401 error (Auth fail)

First I send a POST request with a login and a password (admin authorization) and get some fields.
Next, I need to send GET request to API (check that client email exists), but I receive CURL error: The requested URL returned error: 401
In Postman GET request is working, what I am doing wrong?
This is my GET Request:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, "www.myurl.com/clients?email=a#a.a");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");
$result = curl_exec($ch);
You have to pass query string in case GET.
try below updated code.
$qry_str = "?email=a#a.a"
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, 'www.myurl.com/clients.php',$qry_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");
$result = curl_exec($ch);

How i can send get parameters in PHP Curl?

i tryed to use:
$qry_str = "ft_dl={$ow_ft_dl}&ft_dl_name={$singername} - $songen ({$ow_quality[$i]}).mp3&ft_dl_cover={$src}&sn_name={$singername}&songname={$songen}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, OW_DL_HOST_FUNC . '?' . $qry_str);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
but when i start the code, i dont have any actions to the target file...
the OW_DL_HOST_FUNC file value like : http://sitename.deve/file.php

PHP Curl wrong headers?

I'm sending content from a PHP Curl script to an API.
I'm using this is to do a POST do my script while passing json headers
$query = new stdClass;
$query->test = 'test';
$query = json_encode($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost');
curl_setopt($ch, CURLOPT_HEADER, ['Content-Type: application/json', 'Content-Length: '.strlen($query)]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$res = curl_exec($ch);
curl_close($ch);
But when I trace what the content type of the request in on the API side, I get
var_dump($_SERVER['CONTENT_TYPE']);
//application/x-www-form-urlencoded
Shouldn't I get this instead?
application/json
You should use CURLOPT_HTTPHEADER instead of CURLOPT_HEADER
CURLOPT_HEADER can be true/false and define whether include header to response or not
FYI:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
These lines are redundant as you are not using https

How to api integeration in core php

I had the payment integration with paywithcapure. They send the sample code for test i used that code its 404 error.
$xml_data2 = 'merchantid=somevalue&token=somevalue&bvn=somevalue';
$URL = "http://flutterwavestaging.com:8080/FlutterwaveRecurringPayments/services/flwcardrecurring/VerifyBvn";
$ch = curl_init($URL);
//curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content type - application/x-www-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
First make sure that url is working fine.Paste your url in browser and see the result.If it's not working then contact them for correct url and if its working fine then try this code :
$xml_data2 = 'merchantid=somevalue&token=somevalue&bvn=somevalue';
$URL = "http://flutterwavestaging.com:8080/FlutterwaveRecurringPayments/services/flwcardrecurring/VerifyBvn";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL); // you've missed this one.
//curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content type - application/x-www-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);

cURL Login to HTTPS site

I have been trying to post login credentials to an https site using cURL and PHP with no luck. Everything works fine for unsecured sites but I can't get it with https. I know the headers details that I am posting are correct (although I mocked them up for the sake of this example). Please help.
<?php
// Initialize cURL
$ch = curl_init('https://secured-example.com/auth.asp');
// Enable HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
// Use SSL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// Set POST parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=myUser&password=myPass');
// Imitate classic browser's behavior - handle cookies
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute 1st request
$store = curl_exec($ch);
// Set file to download
curl_setopt($ch, CURLOPT_URL, 'https://secured-example.com/file.pdf');
// Execute 2nd request (file download)
$content = curl_exec($ch);
curl_close($ch);
?>
Export the certificate.
Upload it to where your script can see it.
Then add:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/path/to/certificateCA.crt");
I used this once to connect to a bank account, hope this helps:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, HSBC_LINK1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$post_data = array('post1' => 'value');
$fields_string = '';
foreach($post_data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
curl_setopt($ch, CURLOPT_POST, count($post_data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$data1 = curl_exec($ch);

Categories