gargantuan
gargantuan

Reputation: 8944

Does anyone know how to convert this Perl script into PHP?

Someone just dumped a perl script on me and now it's my problem. I know nothing about Perl. Here's the script.

#! /usr/bin/perl
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
$ua = LWP::UserAgent->new;

my $req = POST 'http://www.someurl.com/aff/', [ search => 'www', errors => 0 ];

my $xml = "<?xml version='1.0' encoding='UTF-8' ?>
<data xmlns='https://www.aff.gov/affSchema' sysID='Adin'
rptTime='2010-06-07T14:10:30.758-07:00' version='2.23'>
<msgRequest to='Co' from='trt' msgType='Data Request' subject='Async'
dateTime='2010-06-07T14:10:30.758-07:00'>
<body>2010-06-07T14:50:06Z</body>
</msgRequest>
</data>";

$req->content( $xml );
my $username = "providedUserName";
my $password = "providedPW";

$req->authorization_basic($username, $password);

print $ua->request($req)->as_string;

As far as I can tell it's creating a HTTP Request object, adding some content and printing the response. Google tells me that I need to install a Perl package to get a HTTPRequest object in PHP, which isn't an option. Is there anyway to do this with cURL or file_get_contents or something?

I'll keep tinkering away, but if anyone knows for sure how to do it, then it'll save me wasting my time at the very least.

Upvotes: 0

Views: 704

Answers (1)

Mauritz Hansen
Mauritz Hansen

Reputation: 4774

This is an HTTP POST request with content type 'text/xml'. I believe you can do this with cURL as follows (example adapted from http://www.infernodevelopment.com/curl-php-send-post-data-background and is untested):

$x = curl_init("http://www.someurl.com/aff/");
curl_setopt($x, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($x, CURLOPT_HEADER, 0);
curl_setopt($x, CURLOPT_POST, 1);
curl_setopt($x, CURLOPT_RETURNTRANSFER, 1);

$xml = "<?xml version='1.0' encoding='UTF-8' ?>
  <data xmlns='https://www.aff.gov/affSchema' sysID='Adin'
    rptTime='2010-06-07T14:10:30.758-07:00' version='2.23'>
      <msgRequest to='Co' from='trt' msgType='Data Request' subject='Async'
        dateTime='2010-06-07T14:10:30.758-07:00'>
          <body>2010-06-07T14:50:06Z</body>
      </msgRequest>
  </data>";
curl_setopt($x, CURLOPT_POSTFIELDS, $xml);

$username = "providedUserName";
$password = "providedPW";
curl_setopt($x, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($x, CURLOPT_USERPWD, "$username:$password");

$data = curl_exec($x);

Upvotes: 6

Related Questions