Skip to Main Content
PHP 5 Kochbuch, Third Edition
book

PHP 5 Kochbuch, Third Edition

by David Sklar, Adam Trachtenberg, Carsten Lucke, Matthias Brusdeylins, Ulrich Speidel, Stephan Schmidt
September 2009
Intermediate to advanced content levelIntermediate to advanced
912 pages
48h 11m
German
O'Reilly Verlag
Content preview from PHP 5 Kochbuch, Third Edition

13.4 Eine URL mit Headern abrufen

Problem

Sie möchten eine URL auslesen, bei der bestimmte Header zusammen mit der Seitenanfrage gesendet werden müssen.

Lösung

Verwenden Sie file_get_contents() mit einem Stream-Kontext:

$contextOptions = array(
    "http" =>
        array ("header"
            => "('X-Faktor: 12\r\nMein-Header: Bob\r\n"));
$context = stream_context_create($contextOptions);
$page = file_get_contents("http://www.example.com/spezieller-header.php", false,
$context);

Als Alternative können Sie die cURL-Erweiterung und die Option CURLOPT_HTTPHEADER verwenden:

$c = curl_init('http://www.example.com/spezieller-header.php'); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_HTTPHEADER, array('X-Faktor: 12', 'Mein-Header: Bob')); $page = curl_exec($c); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP programmieren unter Windows

PHP programmieren unter Windows

Arno Hollosi
CSS Kochbuch, 3rd Edition

CSS Kochbuch, 3rd Edition

Joergen Lang, Christopher Schmitt

Publisher Resources

ISBN: 9783868993271Purchase book