EventBufferEvent::connectHost

(PECL event >= 1.2.6-beta)

EventBufferEvent::connectHostConnects to a hostname with optionally asyncronous DNS resolving

Beschreibung

public EventBufferEvent::connectHost(
     EventDnsBase $dns_base ,
     string $hostname ,
     int $port ,
     int $family = EventUtil::AF_UNSPEC
): bool

Resolves the DNS name hostname, looking for addresses of type family ( EventUtil::AF_* constants). If the name resolution fails, it invokes the event callback with an error event. If it succeeds, it launches a connection attempt just as EventBufferEvent::connect() would.

dns_base is optional. May be null, or an object created with EventDnsBase::__construct() . For asyncronous hostname resolving pass a valid event dns base resource. Otherwise the hostname resolving will block.

Hinweis:

EventDnsBase is available only if Event configured --with-event-extra ( event_extra library, libevent protocol-specific functionality support including HTTP, DNS, and RPC ).

Hinweis:

EventBufferEvent::connectHost() requires libevent-2.0.3-alpha or greater.

Parameter-Liste

dns_base

Object of EventDnsBase in case if DNS is to be resolved asyncronously. Otherwise null.

hostname

Hostname to connect to. Recognized formats are:

www.example.com (hostname)
 1.2.3.4 (ipv4address)
 ::1 (ipv6address)
[::1] ([ipv6address])

port

Port number

family

Address family. EventUtil::AF_UNSPEC , EventUtil::AF_INET , or EventUtil::AF_INET6 . See EventUtil constants .

Rückgabewerte

Gibt bei Erfolg true zurück. Bei einem Fehler wird false zurückgegeben.

Beispiele

Beispiel #1 EventBufferEvent::connectHost() example

<?php
/* Read callback */
function readcb($bev$base) {
    
//$input = $bev->input; //$bev->getInput();

    //$pos = $input->search("TTP");
    
$pos $bev->input->search("TTP");

    while ((
$n $bev->input->remove($buf1024)) > 0) {
        echo 
$buf;
    }
}

/* Event callback */
function eventcb($bev$events$base) {
    if (
$events EventBufferEvent::CONNECTED) {
        echo 
"Connected.\n";
    } elseif (
$events & (EventBufferEvent::ERROR EventBufferEvent::EOF)) {
        if (
$events EventBufferEvent::ERROR) {
            echo 
"DNS error: "$bev->getDnsErrorString(), PHP_EOL;
        }

        echo 
"Closing\n";
        
$base->exit();
        exit(
"Done\n");
    }
}

$base = new EventBase();

$dns_base = new EventDnsBase($baseTRUE); // We'll use async DNS resolving
if (!$dns_base) {
    exit(
"Failed to init DNS Base\n");
}

$bev = new EventBufferEvent($base/* use internal socket */ NULL,
    
EventBufferEvent::OPT_CLOSE_ON_FREE EventBufferEvent::OPT_DEFER_CALLBACKS,
    
"readcb"/* writecb */ NULL"eventcb"$base
);
if (!
$bev) {
    exit(
"Failed creating bufferevent socket\n");
}

//$bev->setCallbacks("readcb", /* writecb */ NULL, "eventcb", $base);
$bev->enable(Event::READ Event::WRITE);

$output $bev->output//$bev->getOutput();
if (!$output->add(
    
"GET {$argv[2]} HTTP/1.0\r\n".
    
"Host: {$argv[1]}\r\n".
    
"Connection: Close\r\n\r\n"
)) {
    exit(
"Failed adding request to output buffer\n");
}

if (!
$bev->connectHost($dns_base$argv[1], 80EventUtil::AF_UNSPEC)) {
    exit(
"Can't connect to host {$argv[1]}\n");
}

$base->dispatch();
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

Connected.
HTTP/1.0 301 Moved Permanently
Location: http://www.google.co.uk/
Content-Type: text/html; charset=UTF-8
Date: Sat, 09 Mar 2013 12:21:19 GMT
Expires: Mon, 08 Apr 2013 12:21:19 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 221
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.co.uk/">here</A>.
</BODY></HTML>
Closing
Done

Siehe auch

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

PHP cURL-Tutorial: Verwendung von cURL zum Durchführen von HTTP-Anfragen

cURL ist eine leistungsstarke PHP-Erweiterung, die es Ihnen ermöglicht, mit verschiedenen Servern über verschiedene Protokolle wie HTTP, HTTPS, FTP und mehr zu kommunizieren. ...

TheMax

Autor : TheMax
Kategorie: PHP-Tutorials

Midjourney Tutorial - Anleitung für Anfänger

Über Midjourney, dem Tool zur Erstellung digitaler Bilder mithilfe von künstlicher Intelligenz, gibt es ein informatives Video mit dem Titel "Midjourney Tutorial auf Deutsch - Anleitung für Anfänger" ...

Mike94

Autor : Mike94
Kategorie: KI Tutorials

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

Getting Database Connection Failed Error in PHP Project

Hi everyone, I’m currently working on an AI and ML-based web application using PHP, and I've hit a roadblock. I’m encountering a “Database ...

Geschrieben von Gast am 27.06.2024 11:31:32
Forum: Off-Topic Diskussionen
How to overcome Safari's iframe cookie block?

Hello I think your cookies are set with SameSite=None; Secure to allow cross-site usage. To implement a custom proxy on the server side to handle ...

Geschrieben von Gast am 27.06.2024 11:04:46
Forum: HTML, JavaScript, AJAX, jQuery, CSS, Bootstrap, LESS
Spielplan für 4 Gruppen zu je 6 Teams auf 2 Feldern

Hey, hallöchen! Du musst die Spiele so durchgehen, dass die Platzierung kontrolliert wechseln, dass jedes Team auf den Plätzen spielt. Hier ist ...

Geschrieben von derH0st am 10.06.2024 14:26:06
Forum: PHP Developer Forum
Does Google ignore common words?

Our website is optimized for "mock web services" and "web service mocks," but Google Webmaster doesn't recognize "web services" (or its variations ...

Geschrieben von TahuluLimia am 10.06.2024 12:26:02
Forum: Webmaster