Jump to content

Archived

This topic is now archived and is closed to further replies.

phpnewb999

HTTPS Connection?

Recommended Posts

Is there a way to establish a https connection with php that is similar to the code below which is written in perl?

#!/usr/bin/perl
use LWP;
use LWP::UserAgent;
use Getopt::Std;
use strict;

use constant BASE_URL => 'https://post.mysite.org/bulk;

# process options
my $opts = {};
getopts('o:f:p', $opts);
my $post = $opts->{'p'};
my $filename = $opts->{'f'};
my $outfile = $opts->{'o'};
unless($filename) {
print "usage: $0 [-p] [-o outfile] -f filename\n".
" options:\n".
" -f - the name of the RSS file to submit (required)\n".
" -p - actually post (otherwise just validate)\n".
" -o - output filename (otherwise results sent to STDOUT)\n\n";
exit(0);
}

# open file
my $content = undef;
open(CFH, "<$filename") || die "can't open $filename for read: $!";
{ local $/ = undef; $content = <CFH>; }
close(CFH);

# prepare request
my $ua = LWP::UserAgent->new();
$ua->agent('SampleBulkPostClient/0.1');
my $post_url = BASE_URL .'/'. ($post? 'post': 'validate');
my $req = HTTP::Request->new( POST => $post_url );
$req->content_type('application/x-www-form-urlencoded');
$req->content($content);

# issue request
my $res = $ua->request($req);

# print result
if($res->is_success()) {
if($outfile) {
open(OFH,">$outfile") || die "can't open $outfile for write: $!";
print OFH $res->content();
close OFH;
}
else {
print $res->content()."\n";
}
}
else {
print "request failed:\n".
$res->status_line()."\n".$res->content()."\n";
}

Share this post


Link to post
Share on other sites
Yes. Using the [a href=\"http://php.net/curl\" target=\"_blank\"]curl[/a] extension.

Share this post


Link to post
Share on other sites
I finally got the Curl extension installed and wrote a little code (which of course is not working :( )

[code]
$fp = fopen("sample.xml", "r")
$url = "https://mysite.com"
$ch = curl_init()
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded"))
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1)  
curl_setopt($ch, CURLOPT_UPLOAD, 1)  
curl_setopt($ch, CURLOPT_INFILE, $fp)  

$result = curl_exec($ch)
curl_close ($ch)
print $result
[/code]

What i need to do is establish a https connection, send a http request with content of "application/x-www-form-urlencoded" containing the sample.xml content.

Any help as to what i'm doing wrong pls.

Thanks

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.