我是一個新的Perl,我有一個腳本獲取我的Linux服務器中的所有數據,處理數據並將其形成json字符串。Perl發佈到php
現在的問題是: 如何在另一個域中的我的php代碼中獲取這些數據。我不知道這種方法,我的導師說要把數據從perl發佈到php,我不知道如何。
請指教。 :D
我是一個新的Perl,我有一個腳本獲取我的Linux服務器中的所有數據,處理數據並將其形成json字符串。Perl發佈到php
現在的問題是: 如何在另一個域中的我的php代碼中獲取這些數據。我不知道這種方法,我的導師說要把數據從perl發佈到php,我不知道如何。
請指教。 :D
要將數據發送到服務器,您可以使用libwww
這一模塊庫在網絡上進行通信。最好的地方可能是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;
這應該給你一個起點,和我以前做的模塊文檔提到更多細節。
這在Perl的許多部分中都有介紹:http://search.cpan.org/~gaas/HTTP-Message-6.06/lib/HTTP/Request/Common.pm – squiguy 2014-09-26 03:02:05
你可以給出一個關於它是如何工作的概述?只需在下面回答,以便我可以隨時獲得支持。謝謝 – waelhe 2014-09-26 03:05:04
我不知道這種方法的概念。 – waelhe 2014-09-26 03:08:59