1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function km200_GetData( $REST_URL )
{
$options = array(
'http' => array(
'method' => "GET",
'header' => "Accept: application/json\r\n" .
"User-Agent: TeleHeater/2.2.3\r\n"
)
);
$context = stream_context_create( $options );
return json_decode(
km200_Decrypt(
file_get_contents(
'http://' . km200_gateway_host . $REST_URL,
false,
$context
)
),
true //Achtung! Hier das true (und drüber das Komma) macht aus dem decodierten Objekt ein Array zur weiteren Bearbeitung)
);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
use LWP::UserAgent; use JSON; my $km200_gateway_host = 'example.org'; sub km200_GetData { my $REST_URL = shift; my $ua = LWP::UserAgent->new; $ua->agent('TeleHeater/2.2.3'); $ua->default_header( Accept => 'application/json' ); my $response = $ua->get( 'http://' . $km200_gateway_host . $REST_URL ); my $data; if ($response->is_success) { $data = decode_json( encode( "utf8", $response->decoded_content ) ); } else { die $response->status_line; } return $data; }
1 2 3 4 5
my $options = HTTP::Headers->new( "Accept" => "application/json", "User-Agent" => "TeleHeater/2.2.3" ); $ua->default_headers($options);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
sub km200_GetData($$$)
{
my $REST_URL = $_[0];
my $km200_gateway_host = $_[1];
my $km200_crypt_key_private = $_[2];
my $ua = LWP::UserAgent->new;
my $options = HTTP::Headers->new(
"Accept" => "application/json",
"User-Agent" => "TeleHeater/2.2.3"
);
$ua->default_headers($options);
my $response = $ua->get( 'http://' . $km200_gateway_host . $REST_URL );
my $data;
Log 3, "km200_GetData: " .$response;
if ($response->is_success) {
$data = decode_json(encode( "utf8", $response->decoded_content ));
#km200_Decrypt(encode( "utf8", $response->decoded_content ), $km200_crypt_key_private));
}
else {
die $response->status_line;
}
Log 3, "km200_GetData: " .$response;
Log 3, "km200_GetData: " .$data;
return $data;
}
1 2 3 4 5 6 7 8 9
use LWP::UserAgent; use HTTP::Request; use Data::Dumper; my $ua = LWP::UserAgent->new; my $request = HTTP::Request->new(GET => 'http://rolfrost.de'); my $response = $ua->request($request); print Dumper $response->header('Content-Type');
1 2 3 4 5
my $hash_ref = km200_GetData("/ox/vorm/berg/blah.blubb"); # die REST-URL die du abrufen willst my %hash = %$hash_ref; use Data::Printer; p(%hash); # gibt Daten aus.
{"id":"/gateway/versionFirmware","type":"stringValue","writeable":0,"recordable":0,"value":"01.05.04"}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function km200_SetData( $REST_URL, $Value )
{
$content = json_encode(
array(
"value" => $Value
)
);
$options = array(
'http' => array(
'method' => "PUT",
'header' => "Content-type: application/json\r\n" .
"User-Agent: TeleHeater/2.2.3\r\n",
'content' => km200_Encrypt( $content )
)
);
$context = stream_context_create( $options );
@file_get_contents(
'http://' . km200_gateway_host . $REST_URL,
false,
$context
);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
sub km200_SetData($)
{
my writevalue = $_;
my $REST_URL = "/folder";
my $km200_gateway_host = "192.168.178.200";
my $ua = LWP::UserAgent->new;
my $options = HTTP::Headers->new(
"Accept" => "application/json",
"User-Agent" => "TeleHeater/2.2.3"
);
$ua->default_headers($options);
$content = {"value"->$writevalue};
$data = km200_Encrypt(encode_json($content));
my $response = $ua->put( 'http://' . $km200_gateway_host . $REST_URL);
}
http://search.cpan.org/~mschilli/libwww-perl-6.08/lib/LWP/UserAgent.pm#ATTRIBUTES$ua->put( $url, \%form )
$ua->put( $url, \@form )
$ua->put( $url, \%form, $field_name => $value, ... )
$ua->put( $url, $field_name => $value,... Content => \%form )
$ua->put( $url, $field_name => $value,... Content => \@form )
$ua->put( $url, $field_name => $value,... Content => $content )
This method will dispatch a PUT request on the given $url, with %form or @form providing the key/value pairs for the fill-in form content. Additional headers and content options are the same as for the get() method.
This method will use the PUT() function from HTTP::Request::Common to build the request. See HTTP::Request::Common for a details on how to pass form content and other advanced features.
my $response = $ua->put( 'http://' . $km200_gateway_host . $REST_URL, $data);
2014-10-24T13:04:06 PerlentaucherIch habe schon mal angefangen aber irgendwie weiss ich trotz Dokmentation nicht, wo ich den $data einbringen soll
2014-10-24T14:29:53 RaubtierWas ist das für eine merkwürdige Funktion, die was mit $_ macht? Wolltest du stattdessen folgendes schreiben?
Und sicher, dass du den Prototypen brauchst?