getting https site through curl - php

I need to get information from https site, it has http authentication, i did some research and i know that i need to use curl so there is my code
$ch = curl_init();
$url = 'https:/....';
$username ='user';
$password ='userspassword';
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSLKEYTYPE,"PEM");
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSLCERT, '/var/www/cert/ows.pem');
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, ' ');
curl_setopt($ch, CURLOPT_SSLKEY, '/var/www/cert/ssl-cert-snakeoil.pem');
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, ' ');
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSLVERSION,3)
and i get "unable to set private key file: '/var/www/cert/ssl-cert-snakeoil.pem' type PEM58" if i dont use any pem i get empty response from server, if i do i cant load them.. where i can find sslcert and sslkey on my system? and how to format them correcly, becouse if i export them directly from firefox it.

Related

PHP cURL with Proxy ( Not SOCKS5 )

I had working code that used my VPN (NordVPN) credentials through their SOCKS5 protocol.
Recently, they have dropped support for SOCKS5 so I have to use their other protocols, however I am unable to get any to work.
All of their available protocols can be seen here
https://nordvpn.com/servers/tools/
- IKEv2/IPSec
- OpenVPN
- Wireguard
- HTTP Proxy (SSL)
Here is my code, attempting to use their "HTTP Proxy (SSL)" protocol
$cookie_file = tempnam("/tmp", "CURLCOOKIE");;
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); // in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP');
curl_setopt($ch, CURLOPT_PROXYPORT, "443");
curl_setopt($ch, CURLOPT_PROXY, "au643.nordvpn.com");
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyUser . ":" . $proxyPass);
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_URL, $Url);
$result = curl_exec($ch);
curl_close($ch);
The error I get is
curl_exec(): supplied resource is not a valid cURL handle resource in ...
I don't really care what protocol it is, just need it to work.
Thanks.
After facing the same issue, I have worked out a solution
$PROXY_HOST = "xxx.xxxxxx.com";
$PROXY_PORT = "89";
$PROXY_USER = "yourproxyusername";
$PROXY_PASS = "yourproxypassword";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_PROXY_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$PROXY_USER:$PROXY_PASS");
curl_setopt($ch, CURLOPT_PROXY, "https://$PROXY_HOST:$PROXY_PORT");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response= curl_exec($ch);
$error = curl_error($ch);
echo $error;
echo $response;
curl_close($ch);
One thing to note is that for NordVPN you have to use the port 89 for HTTP Proxy (SSL) and the username and password is not your account username and password, but your proxy specific credentials that you can find on your account page.
Also dont quote me on this, but I think you need to use PHP 7.3+ for this to work.
we can use it with protonvpn or vpnbook ?

Login not working on Heroku with cURL and PHP

I have a simple PHP script that make a login to desired webpage. It is working OK on my localhost (wamp server), but when I run it on Heroku I get response like this (link: image link:)
Script is simple, just like that:
<?php
$username = "username";
$password = "password";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "url");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( 'user[username]'=>$username, 'user[password]'=>$password)));
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$content = curl_exec($ch);
echo $content;
?>
I don't know what am I doing wrong. Is it something wrong with Heroku or is just luck that code is working OK on my wamp server? I also tried running this script on other free hosting pages but they don't support CURLOPT_FOLLOWLOCATION parameter and so script is not working.
The text you're seeing at the top of the page is the header that you requested with :
curl_setopt ($ch, CURLOPT_HEADER, true);
Also, CURLOPT_POST is implicit when you use CURLOPT_POSTFIELDS, not need for it.
You're code should look something like:
<?php
$username = "username";
$password = "password";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.yoursite.com");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( 'user[username]'=>$username, 'user[password]'=>$password)));
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$content = curl_exec($ch);
echo $content;
Looks like that page that I was trying to post to has country restricted IP.

login into website using the username and password in php curl

I'm facing with error which is not resolved from 3 days . let me know where i'm going wrong . Like wise i need to login into portal and redirect it to next webpage .
url="http://gis.lntecc.com/BWSSBLnT/login.aspx?ReturnUrl=%2fbwssblnt%2fScada.aspx%3ffield1%3dKathriguppe%2cSW2DM0402%2c235505H073%2c450&field1=Kathriguppe,SW2DM0402,235505H073,450";
$useragent="xyz";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'abc:123');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'Login1_Username=abc&Login1_Password=123');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
echo $store;
curl_close ($ch);
The post field names on the website are not the same in your code.
The proper names are Login1$UserName and Login1$Password, you need to look at the name attribute, not the ID.
That being the case, your code should be:
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'Login1$UserName=abc&Login1$Password=123');
hey guys easy way to login into the webpages using php script is just removing url and following with the thing
header("Location: http://gis.lntecc.com/bwssblnt/Scada.aspx? field1=Kathriguppe%2cSW2DM0402%2c235505H073%2c450")

XML Request Using CURL

I am using curl library to get the XML but getting error of connection with host. Following is my code, I have just removed the credentials.
$url="https://interface.callport.net:8080/P-RVWR-drcm01-cti/call/2142100570";
$curl_post_data = array(
"query_type" => 'caller_info',
"dnis" => '8883874944',
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, base64_encode('webapi#fastfix:color43t'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_data);
$result = curl_exec($ch);
if(curl_errno($ch))
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo $result;
}
curl_close($ch);
It's SSL so this usually fixes the problem. cURL does not verify the certificate then.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
[edit] I solved the problems. First the SSL as described above, then I removed the base64 encoding of the username and password, changed so the data is sent by GET instead of POST and lastly fixed the headers.
$url="https://interface.callport.net:8080/P-RVWR-drcm01-cti/call/2142100570?query_type=caller_info&dnis=8883874944";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'webapi#fastfix:color43t');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

login to any site using curl in php

login to a website using curl php
I have tried
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.sitename.com");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_exec($ch);
curl_close($ch);
But this does not log in .
curl_setopt($ch, CURLOPT_POSTFIELDS, true); does not make sense.
You may have CURLOPT_POSTFIELDS and CURLOPT_POST mixed up. The correct way is:
curl_setopt($ch, CURLOPT_POST, true);
Check the manual here: http://no.php.net/manual/en/function.curl-setopt.php
Correct Code should be :
NOTE : change the values for userName an password as required by the login form
for the site.
$postField = "userName = abc&password=xyz";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.sitename.com");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_exec($ch);
curl_close($ch);`

Categories