有谁知道如何将这个Perl脚本转换为PHP?

时间:2011-03-09 18:28:49

标签: php perl http

有人刚刚给我发了一个perl脚本,现在这是我的问题。我对Perl一无所知。这是脚本。

#! /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;

据我所知,它正在创建一个HTTP Request对象,添加一些内容并打印响应。谷歌告诉我,我需要安装一个Perl包来获取PHP中的HTTPRequest对象,这不是一个选项。无论如何使用cURL或file_get_contents或其他东西吗?

我会继续修补,但如果有人知道该怎么做,那么至少可以让我浪费时间。

1 个答案:

答案 0 :(得分:6)

这是内容类型为“text / xml”的HTTP POST请求。我相信您可以使用cURL执行此操作,如下所示(示例改编自http://www.infernodevelopment.com/curl-php-send-post-data-background并且未经测试):

$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);