1

It would be great if someone could help convert this function from Php to Perl, basically this function is meant to get contents of a https url. Right now I'm not good with Perl, I haven't learnt on how to use cURL just yet.

function curl_get_content($url)
                    {
                    $handle = curl_init();
                    curl_setopt($handle, CURLOPT_URL, $url);
                    curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt ($handle, CURLOPT_SSL_VERIFYPEER, 0);
                    curl_setopt($handle, CURLOPT_SSLVERSION, 3); 
                    $data = curl_exec($handle);
                    curl_close($handle);
                    return $data;
                    }

Thanks!

EDIT:

I attempted to use this

my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });  
 my $header = HTTP::Request->new(GET => $URL);  
 my $request = HTTP::Request->new('GET', $URL, $header);  
 my $response = $ua->request($request);  
 if ($response->is_success){  
     print "URL:$URL\nHeaders:\n";  
     print $response->headers_as_string;  
 }elsif ($response->is_error){  
     print "Error:$URL\n";  
     print $response->error_as_HTML;  
 }

But it keeps giving me server error: 500.

5
  • search.cpan.org/~szbalint/WWW-Curl-4.15/lib/WWW/Curl.pm Commented Dec 31, 2013 at 19:57
  • You can also do that with LWP. Commented Dec 31, 2013 at 20:04
  • I did try with LWP, but it doesn't seem to work the way I want it to, unless I'm doing it wrong? sub getContent { my ($URL) = @_; my $agent = LWP::UserAgent->new(env_proxy => 0,keep_alive => 1, timeout => 30); my $header = HTTP::Request->new(GET => $URL); my $request = HTTP::Request->new('GET', $URL, $header); my $response = $agent->request($request); return $response; } Commented Dec 31, 2013 at 20:05
  • 2
    @user2524169: Why don't you add your attempt to your question? Multi-line code in a comment is a PITA to read (which is why i typically don't bother :P ), and there might be a simple fix for what you already have. Commented Dec 31, 2013 at 20:29
  • Are there any SSL-options you need to set? If not and you just want the contents as a string, you can simply use LWP::Simple: use LWP::Simple; print get("https://github.com"); Commented Dec 31, 2013 at 23:10

1 Answer 1

1

My suspicion is that you're not setting the headers correctly so when the server is receiving your request and trying to process the headers, it's not understanding the content and just giving up (giving you the 500 error.) In your code, you're setting $header to a HTTP::Request object but that should actually be a HTTP::Headers object. Set that to:

my $header = HTTP::Headers->new();
$header->header( 'Content-Type' => 'text/plain' ); # set content-type to what you need
# you can add additional headers if needed
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.