Perl post to php


Perl post to php

我是Perl的新手,我有一个脚本可以获取linux服务器中的所有数据,处理数据并将其形成json字符串。

问题是:如何在另一个域的php代码中获取这些数据。"我不知道这种方法,"我的导师说,"把数据从perl发布到php,我不知道怎么做。"。

请告知D

要将数据发送到服务器,可以使用libwww,这是一个用于跨web通信的模块库。最好的起点可能是LWP Cookbook,里面有一些常用的食谱。您的场景,将json数据发布到php脚本,可以通过使用HTTP::Request创建请求并使用LWP::UserAgent:发送来处理

use strict;
use warnings;
use feature ':5.10';
use LWP::UserAgent;
use JSON;
# gather your data
my $data = prepare_data();
# Create a POST request with the URL you want your data going to
my $req = HTTP::Request->new(POST => "http://api.example.com/");
# set the content type as JSON
$req->content_type('application/json');
# encode the json, add it to the request
$req->content( encode_json $data );
# print out the request object as text
say $req->as_string;
# Create a user agent object
my $ua = LWP::UserAgent->new;
# send the request using LWP::UserAgent's request method
my $response = $ua->request($req);
# see what the response was
# LWP::UA has a handy is_success method for checking this
if (! $response->is_success) {
    die "LWP request failed! " . $response->status_line;
}
# print the whole response
say $response->as_string;
# get the contents of the response
my $content = $response->decoded_content;

这应该给你一个开始,我提到的模块的文档有更多的细节。