I'm running PHP 5.6.36 running on windows server 12R2 under IIS 8.5
Whenever I call this code I get an "error code: 6" echoed and an empty $responseText, although I'm able to reach this address with the browser.
I've tried toggling the CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST to true settings and using https:// addresses, but to no avail. Don't get why I can reach these addresses through the browser, but not from the command line.
function genericGet($url,$userName,$passWord){
//overriding $url for the purposes of test
$url = "http://www.google.com";
$reqDefaults = array(
CURLOPT_URL => $url,
CURLOPT_POST => false,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERNAME => $userName,
CURLOPT_PASSWORD => $passWord
);
$curlReq = curl_init();
$responseText = curl_exec($curlReq);
echo "error code:".curl_errno($curlReq)."\r\n";
curl_close($curlReq);
return $responseText;
}
Ok so I finally worked this out. There was an application proxy running on the server. I checked the internet settings in Firefox and found the proxy address then added the CURLOPT_PROXY setting to my setopt array and it worked. Final code looked like this:
function genericGet($url,$userName,$passWord){
//overriding $url for the purposes of test
$url = "http://www.google.com";
$reqDefaults = array(
CURLOPT_URL => $url,
CURLOPT_POST => false,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERNAME => $userName,
CURLOPT_PASSWORD => $passWord,
CURLOPT_PROXY => 'proxyaddress:8080'
);
$curlReq = curl_init();
$responseText = curl_exec($curlReq);
echo "error code:".curl_errno($curlReq)."\r\n";
curl_close($curlReq);
return $responseText;
}
Related
I'm getting 410 - unregistered failures. Even I am deleting my unregistered tokens. It was working fine for years and recently facing more number of issues.
I am passing over the bundle id properly.
if (!defined('CURL_HTTP_VERSION_2_0')) {
define('CURL_HTTP_VERSION_1_0', 3);
}
$url = "{$base_url}/3/device/{$appIosRegId}";
// headers
$headers = array(
"apns-topic: {$app_bundle_id}",
'Authorization: bearer ' . $jws
);
// other curl options
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
CURLOPT_URL => $url,
CURLOPT_PORT => 443,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $varData,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_HEADER => 1,
CURLOPT_SSL_VERIFYPEER => false
));
I have not made changes related notifications and cross checked with all the programs. And everything set was proper only.
Does this needs any upgrade?
My current curl version is curl 7.83.1 (Windows) Release-Date: 2022-05-13
Have made and deployed an app on an AWS EC2 instance, and accessing the app via ip_address:port, where ip_address is the elastic ip associated with the instance.
I am able to make API calls to this app using postman, and via cmd.
However, when I try to make POST request using PHP Curl on Wordpress (hosted on Bluehost), I keep getting the
Failed to connect to ip_address port XXXX after 0 ms: Connection refused.
Any ideas of what I might be missing?
'''php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http:ip_address:8000/payments/accounts/v1',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
CURLOPT_TIMEOUT => 1000,
CURLOPT_CONNECTTIMEOUT => 1000,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_CUSTOMREQUEST => 'POST',));
print_r(curl_error($curl));
curl_exec($curl);
curl_close($curl);
'''
Getting this response
'''code
Failed to connect to id_address port 8000 after 0 ms: Connection refused
'''
Using the redacted ip and a slightly modified version of your code as follows:
$url='http://X.XXX.XX.XX:8000/payments/accounts/v1';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 1000,
CURLOPT_CONNECTTIMEOUT => 1000,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POST=>true
)
);
$res=curl_exec( $curl );
curl_close( $curl );
printf('<pre>%s</pre>',print_r( $res, true ));
Yields:
{"detail":"Authentication credentials were not provided."}
Perhaps if you were to actually include and submit some post data in the CURLOPT_POSTFIELDS option you might find a different response.
I want to download a repository file from github after that the user doing an action. The code will not work as expected. A .zip file is created when the code run but the resulting zip file is blank. How I can fix this?
$url = "https://github.com/$u/$repo/archive/master.zip";
$ch = curl_init();
$f = fopen(__DIR__.'/master.zip', 'w+');
$opt = [
CURLOPT_URL => $url,
CURLOPT_FILE => $f,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
];
curl_setopt_array($ch, $opt);
$file = curl_exec($ch);
curl_close($ch);
fclose($f);
Remove CURLOPT_RETURNTRANSFER => true from your code or put it before CURLOPT_FILE.
This is known interaction as CURLOPT_FILE depends on CURLOPT_RETURNTRANSFER being set.
Currently I have the following code.
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('simple_html_dom.php');
$df = upload('username','password','song.mp3','song.mp3');
$df = json_decode($df, true);
function upload($login,$password,$filename,$file) {
$ch = curl_init();
$postData = array(
'op' => 'login',
'login' => $login,
'password' => $password,
'returnto' => '/'
);
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://dopefile.pk/login.html',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEJAR => 'cookie.txt'
));
$output = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "http://dopefile.pk/?op=upload");
$output = curl_exec($ch);
$html = str_get_html($output);
$ret1 = $html->find('form[id=uploadfile]');
$ret = $html->find('form[id=uploadfile] input');
foreach($ret AS $r) {
$newPost[$r->name]=$r->value;
}
$newPost['file_0'] = '#'.$file.';filename='.$filename;
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://dopefile.pk/cgi-bin/upload.cgi?upload_type=file',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $newPost,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEJAR => 'cookie.txt'
));
return $output = curl_exec($ch);
}
Which is trying to upload to the file sharing website the file song.mp3 which is within my directory, and then return the file sharing websites link (if you download the simple_html_dom.php files and try and run the code above, you can see that it returns no errors or returns no file sharing link + does not upload to my account on the website...
The cookies file updates with the cookies that show I successfully logged in, how ever I can't get the actual file link returned can anyone help me please?
I appreciate this has been asked a number of times but after looking through the answers I'm still getting the following error:
Curl error: Peer certificate cannot be authenticated with known CA certificates
I have the below code, which works on a non-https version of the site (different server), however, it won't work on the live site running https. My assumption is that it's trying to verify against the SSL certificate on the site, rather than the .pem file specified in $cert_path.
I'll be honest, I'm a bit confused between SSLCERT and the CAINFO however I have added the following to my php.ini file and checked to make sure they're set correctly, which they are.
// php.ini
curl.cainfo = "/path/to/.pem";
openssl.cafile = "/path/to/.pem";
As I said before, the following works on the stage version of the site, just not the live one. Any help would be greatly appreciated.
// php curl snippet
$cert_path = '/path/to/.pem';
$options = array(
CURLOPT_SSL_VERIFYHOST => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSLCERT => $cert_path,
CURLOPT_SSLCERTPASSWD => $password,
CURLOPT_CAINFO => $cert_path,
CURLOPT_HTTPAUTH => CURLAUTH_ANY,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $xml_post_string,
CURLOPT_HTTPHEADER => $headers
);
curl_setopt_array( $ch, $options );
If I set VERIFYPEER to false it does connect but my assumption is that this wouldn't be passing the data securely?
Thank you.
I've managed to get this working however there are a few things to note which may help someone in the future.
The site was hosted with 123reg and I was informed by their support that third-party SSL certificates are not accepted. Although I was told this ultimately my code below did work.
I regenerated my .pem file so this too could have worked in my favour.
My final code as as follows: I also stripped out the php.ini code entirely.
// These two variables will differ depending on your payload for Curl
$xml_post_string = '...';
$headers = array();
// This will differ based on your endpoint
$url = 'endpoint.url';
$password = 'password';
$cert_path = '/path/to/.pem';
$ch = curl_init();
$options = array(
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSLCERT => $cert_path,
CURLOPT_SSLCERTTYPE => "PEM",
CURLOPT_SSLCERTPASSWD => $password,
CURLOPT_HTTPAUTH => CURLAUTH_ANY,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $xml_post_string,
CURLOPT_HTTPHEADER => $headers
);
curl_setopt_array( $ch, $options );
$response = curl_exec($ch);
curl_close($ch);