<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ROAE-Blog &#187; post</title>
	<atom:link href="http://blog.root-of-all-evil.com/tag/post/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.root-of-all-evil.com</link>
	<description>Studium, Codeing und Gedachtes</description>
	<lastBuildDate>Mon, 30 Jan 2012 22:58:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>fsockopen statt file_get_contents für HTTP-Requests</title>
		<link>http://blog.root-of-all-evil.com/2010/04/fsockopen-statt-file_get_contents-fur-http-requests/</link>
		<comments>http://blog.root-of-all-evil.com/2010/04/fsockopen-statt-file_get_contents-fur-http-requests/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 13:00:05 +0000</pubDate>
		<dc:creator>Felix</dc:creator>
				<category><![CDATA[Codeschnipsel]]></category>
		<category><![CDATA[Erklärungen]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[post]]></category>

		<guid isPermaLink="false">http://blog.root-of-all-evil.com/?p=737</guid>
		<description><![CDATA[<a href="http://blog.root-of-all-evil.com/2010/04/fsockopen-statt-file_get_contents-fur-http-requests/" title="fsockopen statt file_get_contents für HTTP-Requests"></a>Bei zahlreichen Webspace-Providern ist die Funktionalität von file_get_contents für http deaktiviert. Es treten dann Fehlermeldungen wie Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in [..] und Warning: file_get_contents([..]) [function.file-get-contents]: failed to open stream: no suitable wrapper &#8230;<p class="read-more"><a href="http://blog.root-of-all-evil.com/2010/04/fsockopen-statt-file_get_contents-fur-http-requests/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://blog.root-of-all-evil.com/2010/04/fsockopen-statt-file_get_contents-fur-http-requests/" title="fsockopen statt file_get_contents für HTTP-Requests"></a><p>Bei zahlreichen Webspace-Providern ist die Funktionalität von file_get_contents für http deaktiviert. Es treten dann Fehlermeldungen wie</p>
<p><em>Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in [..] </em>und<em></em></p>
<p><em>Warning: file_get_contents([..]) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in [..]</em></p>
<p>auf. Mit folgender Funktion kann man das Verhalten von file_get_contents jedoch emulieren.</p>
<p><span id="more-737"></span></p>
<p>Die Funktion <em>get_contents_by_uri</em> benutzt <em>fsockopen </em>um Inhalte zu laden:</p>
<pre class="php" title="code">/**
 * Gets contents of any webside by an URI.
 * @author http://blog.root-of-all-evil.com
 * @param String $uri
 */
function get_contents_by_uri($uri) {
	$uriElem = parse_url ( $uri );
	$fp = @fsockopen ( $uriElem ['host'], 80, $errno, $errstr, 10 );

	if (! $fp) {
		throw new Exception ( "Could not create socket: '" . $errnstr . "' (" . $errno . ")." );
	}

	$request = "GET " . $uriElem ['path'] . (isset ( $uriElem ['query'] ) ? "?" . $uriElem ['query'] : "") . " HTTP/1.1\r\n";
	$request .= "Host: " . $uriElem ['host'] . "\r\n";
	$request .= "Connection: Close\r\n\r\n";

	fwrite ( $fp, $request );
	$response = "";
	while ( ! feof ( $fp ) ) {
		$response .= fgets ( $fp, 128 );
	}
	fclose ( $fp );

	// split headers from data
	$responseSplit = explode ( "\r\n\r\n", $response, 2 );

	return $responseSplit [1];
}</pre>
<h1>Erklärung</h1>
<p><strong>Socket erstellen</strong></p>
<p>Die übergebene URI wird über die PHP-Funktion<em> parse_url</em> in ihre Teilmengen zerlegt. Anschließend wird ein Socket erstellt für den entsprechenden Host, auf Port 80 mit einem Timeout von 10 Sekunden. Fehlermeldungen unterdrücken wir durch das <em>@</em>, da wir explizit nach Erfolg abfragen und ggf. eine Ausnahme werfen.</p>
<p><strong>Anfrage schicken, Antwort lesen</strong></p>
<p>Danach wird über <em>fwrite </em>eine entsprechende GET-Anfrage geschickt, welche auch &#8211; falls vorhanden &#8211; die Parameter der URI und nicht nur den Pfad beachtet. Die darauffolgende Antwort wird in die Variable <em>response </em>gelesen.</p>
<p><strong>Antwort bearbeiten und zurückgeben</strong></p>
<p>Da diese neben dem eigentlichen Inhalt auch den Antwortheader enthält wird die Funktion <em>explode </em>benutzt um den Inhalt davon abzuschneiden. Durch den dritten Parameter bei <em>explode </em>mit dem Wert 2, stellen wir sicher, dass nur einmalig an Hand einer Leerzeile getrennt wird wie Sie sich zwischen Antwort-Header und HTML-Code befindet und nicht mehrmals, da natürlich auch Leerzeilen im HTML-Code selbst auftreten können.</p>
<p>Das zweite Element von <em>responseSplit </em>wird zurückgegeben, <em>responseSplit[0]</em> würde entsprechend den Antwort-Header liefern.</p>
<h1>Beispielaufrufe</h1>
<p>Der einfachste Aufruf:</p>
<pre class="php" title="code">echo get_contents_by_uri ( 'http://www.google.de/' );</pre>
<p>Aufruf mit Parametern:</p>
<pre class="php" title="code">echo get_contents_by_uri ( 'http://www.google.de/search?hl=de&amp;site=&amp;q=test' );</pre>
<p>Aufruf mit Ausnahmebehandlung:</p>
<pre class="php" title="code">try{
    echo get_contents_by_uri ( 'http://does_not_exist_.com/bogus.html' );
}
catch(Exception $e)
{
    echo "get_contents_by_uri: ".$e-&gt;getMessage();
}</pre>
<p>Weitere Links:</p>
<ul>
<li><a href="http://de2.php.net/parse_url">PHP &#8211; Funktion parse_url</a></li>
<li><a href="http://de2.php.net/fsockopen">PHP &#8211; Funktion file_get_contens</a></li>
<li><a href="http://de2.php.net/fsockopen">PHP &#8211; Funktion fsockopen</a></li>
<li><a href="http://de2.php.net/manual/en/function.explode.php">PHP &#8211; Funktion explode</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.root-of-all-evil.com/2010/04/fsockopen-statt-file_get_contents-fur-http-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bookmarklet erstellen: Methode bei HTML-Formularen ändern</title>
		<link>http://blog.root-of-all-evil.com/2010/02/bookmarklet-erstellen-methode-bei-html-formularen-andern/</link>
		<comments>http://blog.root-of-all-evil.com/2010/02/bookmarklet-erstellen-methode-bei-html-formularen-andern/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 17:25:12 +0000</pubDate>
		<dc:creator>Felix</dc:creator>
				<category><![CDATA[Codeschnipsel]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[bookmarklet]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[form method]]></category>
		<category><![CDATA[get]]></category>
		<category><![CDATA[post]]></category>

		<guid isPermaLink="false">http://blog.root-of-all-evil.com/?p=441</guid>
		<description><![CDATA[<a href="http://blog.root-of-all-evil.com/2010/02/bookmarklet-erstellen-methode-bei-html-formularen-andern/" title="Bookmarklet erstellen: Methode bei HTML-Formularen ändern"></a>Als Entwickler ist es oft wichtig zu sehen welche Daten übetragen werden. Gründe hierfür sind z.B. Sicherheit und Debuging. Folgendes Bookmarklet, wandelt alle Formulare auf der Seite von post- auf get-Formulare um. Nachstehende Funktion ermittelt alle Form-Tags auf der Seite. &#8230;<p class="read-more"><a href="http://blog.root-of-all-evil.com/2010/02/bookmarklet-erstellen-methode-bei-html-formularen-andern/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://blog.root-of-all-evil.com/2010/02/bookmarklet-erstellen-methode-bei-html-formularen-andern/" title="Bookmarklet erstellen: Methode bei HTML-Formularen ändern"></a><p>Als Entwickler ist es oft wichtig zu sehen welche Daten übetragen werden. Gründe hierfür sind z.B. Sicherheit und Debuging. Folgendes Bookmarklet, wandelt alle Formulare auf der Seite von post- auf get-Formulare um.</p>
<p><span id="more-441"></span></p>
<p>Nachstehende Funktion ermittelt alle Form-Tags auf der Seite. Sie überprüft anschließend, ob das method-Attribute vorhanden ist, wenn nicht wird es erstellt und der Wert &#8220;get&#8221; zugewiesen. Sollte das method-Attribute vorhanden sein, und den Wert &#8220;post&#8221; besitzen, wird dieser auf &#8220;get&#8221; geändert.</p>
<pre class="javascript" title="code">function changePostToGet()
{
	count = 0;
	els = document.getElementsByTagName('form');
	for(i=0;i&lt;els.length;i++)
	{
		if(!els[i].getAttribute('method'))
		{
			attrib = document.createAttribute('method');
			attrib.nodeValue = 'get';
			els[i].setAttributeNode(attrib);

			count ++;
		}
		else if(els[i].getAttribute('method').toLowerCase()=='post')
		{
			els[i].setAttribute('method', 'get');

			count ++;
		}
	}

	alert(count+' form(s) transformed.');
}</pre>
<p>Da es nun praktisch schlecht ist, diesen Code auf jeder Seite unterzubringen, auf welchen man ihn benötigen würde, erstellen wir uns daraus ein <a href="http://de.wikipedia.org/wiki/Bookmarklet">Bookmarklet</a>.</p>
<p>Der Funktionsrumpf wird hierzu in den <a href="http://javascriptcompressor.com/">Javascript Compressor</a> kopiert, welcher daraus folgenden String erstellt:</p>
<pre class="javascript" title="code">count=0;els=document.getElementsByTagName('form');for(i=0;i&lt;els.length;i++){if(!els[i].getAttribute('method')){attrib=document.createAttribute('method');attrib.nodeValue='get';els[i].setAttributeNode(attrib);count++}else if(els[i].getAttribute('method').toLowerCase()=='post'){els[i].setAttribute('method','get');count++}}alert(count+' form(s) transformed.');</pre>
<p>Um das Bookmarklet jetzt z.B. im Firefox zu benutzen, wird über den Lesezeichenmanager in der Lesezeichen-Symbolleiste ein neuer Favorit erstellt.  Bei Adresse tragen wir <em>javascript:&lt;code&gt;</em> ein wobei <em>&lt;code&gt;</em>durch den obigen String ersetzt wird.</p>
<div id="attachment_442" class="wp-caption aligncenter" style="width: 387px"><a href="http://blog.root-of-all-evil.com/wp-content/uploads/2010/02/22.02.jpg" rel="lightbox[441]"><img class="size-full wp-image-442" title="Bookmarklet in Firefox" src="http://blog.root-of-all-evil.com/wp-content/uploads/2010/02/22.02.jpg" alt="" width="377" height="265" /></a><p class="wp-caption-text">Erstellen eines neuen Bookmarklets in Firefox</p></div>
<p>Nach dem Klick auf &#8220;Hinzufügen&#8221; werden nun alle Formulare auf der Seite welche man gerade betrachtet, von post in get konvertiert.</p>
<p>Alternativ reicht es auch diesen Link in die Favoritenleiste des Browsers zu ziehen: <a style="cursor: move;" title="PostToGet Bookmarklet" href="javascript:count=0;els=document.getElementsByTagName('form');for(i=0;i&lt;els.length;i++){if(!els[i].getAttribute('method')){attrib=document.createAttribute('method');attrib.nodeValue='get';els[i].setAttributeNode(attrib);count++}else if(els[i].getAttribute('method').toLowerCase()=='post'){els[i].setAttribute('method','get');count++}}alert(count+' form(s) transformed.');">PostToGetBookmarklet</a></p>
<p>Wird dann ein solches Formular abgeschickt, ist schnell ersichtlich welche Parameter welchen Wert besitzen &#8211; ohne das Änderungen an der Webseite selbst vorgenommen werden müssen.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.root-of-all-evil.com/2010/02/bookmarklet-erstellen-methode-bei-html-formularen-andern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

