13.5 Eine URL über eine beliebige HTTP-Methode abrufen
Problem
Sie möchten eine URL über eine exotischere Methode als get
oder post
anfordern, put
oder delete
beispielsweise.
Lösung
Setzen Sie, wie beim Einsatz von post
, die Stream-Kontextoptionen method
und content
, wenn Sie den http
-Stream nutzen, wie Sie es in Listing 13.1 sehen.
<?php $url = 'http://www.example.com/put.php'; // Der Request-Body in beliebigem Format $body = '<menu> <dish type="appetizer">Hühnersuppe</dish> <dish type="main course">Frittiertes Affenhirn</dish> </menu>'; $options = array('method' => 'PUT', 'content' => $body); // Den Stream-Kontext erzeugen $context = stream_context_create(array('http' => $options)); // Den Kontext an file_get_contents() übergeben print file_get_contents($url, ...
Get PHP 5 Kochbuch, Third Edition now with O’Reilly online learning.
O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.