Unable to make https requests via proxy using,php and curl - php

We are Unable to make https requests via proxy using php and curl. We tried to make a simple curl request from PHP to https://google.com and we get message 'Request could not be processed.Invalid response received by proxy or gateway server'.The same request to http:/google.com works fine. We are also able to successfully call any https url from curl command line. below is our curl request. Proxy doesn't require login and we have openssl installed in PHP. Any replies are appreciated.
$url='https://google.com';
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_PROXY, 'some proxy');
curl_setopt($handle, CURLOPT_PROXYPORT, 80);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_VERBOSE, TRUE);
$data = curl_exec($handle);

Try changing:
curl_setopt($handle, CURLOPT_PROXYPORT, 80);
to:
curl_setopt($handle, CURLOPT_PROXYPORT, 443);
Since https traffic is on port 443.

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
It works for me .

Related

Http proxy to connect with https website php

Is it possible to connect to an https website with an http proxy(which doesn't support https)?
I know the data will not be secure and all that, But is it possible anyway ?
I am using the following code, It works with http://website.com But doesn't work with the https://website.com
$proxy = "proxy which doesn't support https"
$url = "https://website.com" //https url
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY,$proxy);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = curl_exec($ch);
Curl throws the following error when I try to connect to HTTP website Received HTTP code 403 from proxy after CONNECT

php curl - allow calls with expired certificate

Using curl to make calls to the server. The connection is made on the protocol https using ssl certificate.
Use the following code.
curl_setopt($ch, CURLOPT_URL, $szUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_CAINFO, $this->aConfig['certificatePath']);
I realized that despite the certificate has expired curl continues to function properly. why? no way to verify the certificate has expired?
what safety problems exposes this thing?
Support for:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
removed with cURL 7.28.1.
Use
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
Should do the trick.

PHP connect to HTTPS site through proxy

I've got the following problem: there is an HTTPS web site, and I need to connect to it through a proxy. Here are my cURL setopts:
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, '100.100.100.100:8080');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo curl_error($ch);
outputs Failed connect to ######.com:8080; No error
Where 100.100.100.100:8080 is a placeholder for a valid HTTPS proxy. This doesn't work. How do I make cURL connect to an HTTPS website through a proxy? I would really like a soultion that would work through not only HTTPS proxies. Also, I would best prefer a method using cURL, but if there is a better way to do it, without cURL, I could use it instead.
Update:
Add
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
It will prevent your HTTP proxy to parse your request headers and to act more transparently - like a tunnel.
initial answer, not interesting
Your code looks OK, and I assume you checked the trivial issues, so the problem is probably that the SSL certificate verification fails. It's the case if the certificate is self signed by example.
Try
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
to allow a request that allows using a self signed certificate.

PHP cUrl - post data doesn't go through proxy

I use this code to login to remote server. Evrything working fine when I don't use proxy. But with proxy it doesn't.
Here is code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $ref_url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt($ch, CURLOPT_PROXY, $proxy.':'.$port);
ob_start();
return curl_exec ($ch); // execute the curl command
ob_end_clean();
curl_close ($ch);
unset($ch);
I try to disable CURLOPT_HTTPPROXYTUNNEL but it didn't help.
So, without proxy I can login fine.
With proxy not.
Proxy is good ad working.
What's the proxy error you get using the code you posted? Does the proxy need authentication?
I was having a same problem , the code works fine without the proxy but with it its returning nothing
, I have done some debugging and found out that actually on most of the servers only the port 80 is allowed , and many of the proxies we use connect through another port
You can unblock the port no for which you have maximum proxies
You can filter the list with only port 80 proxies
Note : Also only use port http, https and socks4 proxies , socks5 proxies runs on udp not on tcp/ip

PHP cURL sending to port 8080

today I am trying to make a curl call to somesite which is listening to port 8080. However, calls like this get sent to port 80 and not 8080 instead:
$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, 8080);
curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$target_response = curl_exec($ch);
curl_close($ch);
am i missing something here?
Just a note, I've had this issue before because the web host I was using was blocking outbound ports aside from the standard 80 and 443 (HTTPS). So you might want to ask them or do general tests. In fact, some hosts often even block outbound on port 80 for security.
Simple CURL GET request: (Also added json/headers if required, to make your life easier in need)
<?php
$chTest = curl_init();
curl_setopt($chTest, CURLOPT_URL, "http://example.com:8080/?query=Hello+World");
curl_setopt($chTest, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Accept: application/json'));
curl_setopt($chTest, CURLOPT_HEADER, true);
curl_setopt($chTest, CURLOPT_RETURNTRANSFER, true);
$curl_res_test = curl_exec($chTest);
$json_res_test = explode("\r\n", $curl_res_test);
$codeTest = curl_getinfo($chTest, CURLINFO_HTTP_CODE);
curl_close($chTest);
?>
Example POST request:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com:8080');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Accept: application/json'));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"Hello" : "World", "Foo": "World"}');
// Set timeout to close connection. Just for not waiting long.
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$curl_res = curl_exec($ch);
?>
Charset is not a required HTTP header and so are the others, the important one is Content-Type.
For the examples I used JSON MIME type, You may use whatever you want, take a look at the link:
http://en.wikipedia.org/wiki/Internet_media_type
Make sure that the php_curl.dll extension is enabled on your php, and also that the ports are open on the target serve.
Hope this helps.
Have you tried to SSH into the server that runs this and verify the iptables rules, and/or attempt to connect via telnet to the target server?
sh
telnet somesite.tld 8080
If nothing else, it will help troubleshoot your problem and eliminate network issues.
First check that you're able to connect using something other than PHP's horrible Curl syntax:
Chrome's Postman is easy to use if you're on your local machine,
else look at using (for linux users)
curl somesite:8080
wget -qO- somesite:8080
Once you've established you can connect, then you can go about the horrible business of using PHP Curl. There are wrappers, but they're flaky that I've found.
Both Curl, Wget or similar can be very easily configured to use GET and POST methods. It's not advisible, but for more than one complicated operation using Curl I've simply given up trying to configure PHP's library correctly and simply dropped to the command line.
THERE ARE SECURITY IMPLICATIONS. You need to take great care to ensure that anything you give it, particularly if it's from a form or an external source, is appropriately escaped.
<?
//Strip out any possible non-alpha-numeric character for security
$stringToSend = preg_replace('[^a-zA-Z]', '', $stringToSend);
$return = shell_exec("curl -X POST -d $stringToSend http://example.com/path/to/resource");
Your web server is misconfigured. The code you provided works for me.
Also, your code can be simpler. Just put the URI into the init call and drop the CURLOPT_PORT and CURLOPT_URL lines:
$ch = curl_init('http://somesite.tld:8080');
Are you sure that the server you intent to join isn't firewalled? And that Selinux is disabled?
Maby you can use
--local-port [-num]
Set a preferred number or range of local port numbers to use for the connection(s). Note that port numbers by nature are a scarce resource that will be busy at times so setting this range to something too narrow might cause unnecessary connection setup failures. (Added in 7.15.2)
Source: http://curl.haxx.se/docs/manpage.html#--ftp-pasv
// Use PHP cURL to make a request to port 8080 on a server
$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, 8080);
curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$target_response = curl_exec($ch);
curl_close($ch);
curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');
PHP cURL sending to port 8080
$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, 8080);
curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$target_response = curl_exec($ch);
curl_close($ch);
curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');
try this option for curl
curl_setopt($ch, CURLOPT_PROXY,"localhost:8080");
or use this
curl_setopt($ch, CURLOPT_PROXY,"yourservername:8080");
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "your-domain-name");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch); `
?>

Categories