Alexander Brock: HTTP: wie muss ein GET über fsockopen aussehen?

Beitrag lesen

Hallo Freunde des gehobenen Forumsgenusses,

  

> > function PostToHost($host, $path, $referer, $data_to_send) {  
> >  $fp = fsockopen($host,80);  
> >  if ($fp === false) return false;  
> >  fputs($fp, "GET $path?$data_to_send HTTP/1.0\r\n");  
> >  fputs($fp, "Host: $host\r\n");  
> >  fputs($fp, "Referer: $referer\r\n");  
> >  fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");  
> ?  fputs($fp, "Content-length: ".strlen($data_to_send)."\r\n");  

Ich weis nicht recht. Die gesendeten Daten sind doch Teil der URL, die stehen nicht im
Body des Requests (der Request hat keinen). Also kann der Request keine Body-Länge haben.

Und das ganze dann in eine rekursive Funktion packen.
Es könnte sich ja eine Umleitung melden o. ä.

Um die Umleitung zu erkennen macht man zuerst einen HEAD-Request.

Lies!
8.1  GET

The GET method means retrieve whatever information (in the form of an
   entity) is identified by the Request-URI. If the Request-URI refers
   to a data-producing process, it is the produced data which shall be
   returned as the entity in the response and not the source text of the
   process, unless that text happens to be the output of the process.

The semantics of the GET method changes to a "conditional GET" if the
   request message includes an If-Modified-Since header field. A
   conditional GET method requests that the identified resource be
   transferred only if it has been modified since the date given by the
   If-Modified-Since header, as described in Section 10.9. The
   conditional GET method is intended to reduce network usage by
   allowing cached entities to be refreshed without requiring multiple
   requests or transferring unnecessary data.

8.2  HEAD

The HEAD method is identical to GET except that the server must not
   return any Entity-Body in the response. The metainformation contained
   in the HTTP headers in response to a HEAD request should be identical
   to the information sent in response to a GET request. This method can
   be used for obtaining metainformation about the resource identified
   by the Request-URI without transferring the Entity-Body itself. This
   method is often used for testing hypertext links for validity,
   accessibility, and recent modification.

There is no "conditional HEAD" request analogous to the conditional
   GET. If an If-Modified-Since header field is included with a HEAD
   request, it should be ignored.

Welcher Parameter hat wo die Slashes?

$host

Hat keine Slashes

$path

Hat Slashes

$referer   --> vollständige URi oder nur URL?

10.13  Referer

The Referer request-header field allows the client to specify, for
   the server's benefit, the address (URI) of the resource from which
   the Request-URI was obtained. This allows a server to generate lists

of back-links to resources for interest, logging, optimized caching,
   etc. It also allows obsolete or mistyped links to be traced for
   maintenance. The Referer field must not be sent if the Request-URI
   was obtained from a source that does not have its own URI, such as
   input from the user keyboard.

Referer        = "Referer" ":" ( absoluteURI | relativeURI )

Example:

Referer: http://www.w3.org/hypertext/DataSources/Overview.html

If a partial URI is given, it should be interpreted relative to the
   Request-URI. The URI must not include a fragment.

Note: Because the source of a link may be private information or
      may reveal an otherwise private information source, it is strongly
      recommended that the user be able to select whether or not the
      Referer field is sent. For example, a browser client could have a
      toggle switch for browsing openly/anonymously, which would
      respectively enable/disable the sending of Referer and From
      information.

$data_to_send  --> urlencoded ?

Die Werte schon. Es empfiehlt sich, als $data_to_send ein assozoiatives Array zu übergeben:

  
$data_result = '';  
foreach ($data as $key=>$value) {  
 $data_result .= $key.'='.urlencode($value).'&';  
}  
$data_result = substr($data_result, 0, -1);  

Was ist mit dem charset?

Das ist doch normalerweise ein HTML-Formular, oder?
Da steht drin, welchen charset man senden soll.
Wenn nicht sendet man den, den die angeforderte Resource hat.
Den bekommt man über den HEAD-Request.

Tom

Hat offensichtlich Brett vorm Kopf *SCNR* (nicht bös gemeint).

Gruß
Alexander Brock

--
Ceterum censeo Carthaginem esse delendam