Can't Connect to Https Site Using Curl. Returns 0 Length Content Instead. What How to Do

Can't connect to HTTPS site using cURL. Returns 0 length content instead. What can I do?

You should also try checking the error messages in curl_error(). You might need to do this once after each curl_* function.

http://www.php.net/curl_error

Getting no content from a HTTPS connection using CURL

Alright, after I'd compiled the library for the nth time, everything seemed fine until I debugged it and it gave me heap errors, suggesting I made a mistake *twitch twitch* again.

Before starting a new compiling session I took another look around the curl.haxx and found an MSVC package. It made me gleeful, because I know how to handle those! I chose the static library, because that's what I wanted.

Though, not everything went smoothly, I had to add some additional linker dependencies for it to work properly.

namely

#pragma comment(lib, "curllib_static.lib")
#pragma comment(lib, "wldap32.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "ssleay32.lib")
#pragma comment(lib, "openldap.lib")
#pragma comment(lib, "libeay32.lib")

Put those in and used the certificate bundle from the other package I downloaded so that I could connect to HTTPS.

curl_easy_setopt(curl, CURLOPT_CAINFO, "_path_\ca-bundle.crt");

I am getting a few warnings though, warnings I've never seen before. They don't mess anything up, but it's got something to do with the PDB file "vc90", something I thought VC++ handled by itself.

Here's the code connecting to https://www.google.se (pragmas removed and added in linker dependencies under project properties) after finally having a proper CURL library.

Here's the MSVC package (SSL Enabled). You can use one of the scripts (perl or VBscript) from this package to make the certificate bundle for you.

Oh, also, if you're using a static library like me, you have to add the preprocessor definition CURL_STATICLIB

#include <iostream>
#include <string>
#include <curl/curl.h>

using namespace std;

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp){
((string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}

int main(){
CURL *curl;
CURLcode res;
string readBuffer;

curl = curl_easy_init();
if(curl){
curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.se");
curl_easy_setopt(curl, CURLOPT_CAINFO, "C:\\libcurl_static\\ca-bundle.crt");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);

cout<<readBuffer<<endl;

system("pause");
}
return 0;
}

Was really my fault, I should've checked the description on the download wizard they have. I only downloaded the latest source from the archives (7.27.0) because I thought I had to compile it myself to get what I wanted.

This whole thing is embarrassing. I learned a lot from it though.

PHP CURL & HTTPS

Quick fix, add this in your options:

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false)

Now you have no idea what host you're actually connecting to, because cURL will not verify the certificate in any way. Hope you enjoy man-in-the-middle attacks!

Or just add it to your current function:

/**
* Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an
* array containing the HTTP server response header fields and content.
*/
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks
);

$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );

$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}

cURL post with PHP failing

Few things that might be preventing your code from working:

1. Array to string conversion

You have declared the body of the request as an array in $body. You need to JSON-encode it before passing it to cURL:

$body = array('Filter' => array('SKU'=>$filters, 'OutputSelector' => $selectors));
$bodyJSON = json_encode($body);
[...]
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyJSON);

2. HTTPS request

If you're getting an empty response, you'll also need to configure cURL for SSL communication. A quick fix for this is to ignore the remote server certificate validity:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Although this works, see this answer for a better solution.

3. Missing Content-Type

When sending raw postfields, some servers need to know what type of data you're sending. This is done by setting a Content-Type header:

$headers = array(
"Content-Type: application/json",
[...]
);

Enable SSL connection for https in cURL PHP (header blank)

You're excluding the body by using curl_setopt($ch, CURLOPT_NOBODY, true);. And I don't think you need to install certificate on your machine. The following few lines will give you everything.

$url = 'https://www.fanniemae.com/content/datagrid/hist_net_yields/cur30.html';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // set url
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); // set browser/user agent
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header'); // get header
curl_exec($ch);

function read_header($ch, $string) {
print "Received header: $string";
return strlen($string);
}

I can't connect to a https site server by Caddy using curl

It turn out that by default Caddy will communication with a minimum of version
of tls 1.1 whereas my host was using 1.0. If you add this to your caddyfile it should work if this is your issue.

example.ie {
tls {
protocols tls1.0 tls1.2 #min max
}
log access.log
errors err.log
proxy / http://myexample.com:3032

}

Why is curl trying to connect to localhost instead of going out to the internet?

It seems your curl is trying to connect to a proxy server instead of connecting to the internet directly.

Check your environment by command env and see if there is any http_proxy.

Also, check if you have some debugging tools like Fiddler or TCPDump or Charles proxy or Jmeter setup, as they may create a proxy server.

You can also run curl with -v to get further info:
curl -v -L https://packages.microsoft.com/keys/microsoft.asc



Related Topics



Leave a reply



Submit