<?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>lemmingzshadow.net &#187; PHP - lemmingzshadow.net - a webdevel&#039;s weblog</title>
	<atom:link href="http://lemmingzshadow.net/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://lemmingzshadow.net</link>
	<description>a webdevel&#039;s weblog</description>
	<lastBuildDate>Fri, 27 Jan 2012 20:18:19 +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>PHP Websocket Server/Client nach Draft hybi-10</title>
		<link>http://lemmingzshadow.net/386/php-websocket-serverclient-nach-draft-hybi-10/</link>
		<comments>http://lemmingzshadow.net/386/php-websocket-serverclient-nach-draft-hybi-10/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 12:16:26 +0000</pubDate>
		<dc:creator>Lemmingz Shadow</dc:creator>
				<category><![CDATA[featured post]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Websockets]]></category>

		<guid isPermaLink="false">http://lemmingzshadow.net/?p=386</guid>
		<description><![CDATA[Vor ein paar Tagen habe ich ja schon einen Websocket-Client hier veröffentlich, welcher Draft hybi-00 unterstützt. Kurz danach veröffentlichte google jedoch eine neue Version von Chrome. In Chrome 14 hat google nun den Websocket-Draft hybi-10 implementiert und dort hat sich vieles grundlegend geändert. Neben einem neuen Handshake werden auch die Datenframes komplett neu codiert. Ich [...]]]></description>
			<content:encoded><![CDATA[<p>Vor ein paar Tagen habe ich ja schon einen Websocket-Client hier veröffentlich, welcher Draft hybi-00 unterstützt. Kurz danach veröffentlichte google jedoch eine neue Version von Chrome. In Chrome 14 hat google nun den <a href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10">Websocket-Draft hybi-10</a> implementiert und dort hat sich vieles grundlegend geändert. Neben einem neuen Handshake werden auch die Datenframes komplett neu codiert.<br />
Ich habe meine Client-Klasse entsprechend angepasst und möchte sie natürlich wieder hier präsentieren, da sie dem ein oder anderen Entwickler sicherlich helfen wird. Desweiteren habe ich die &#8220;Connection-Klasse&#8221; des Websocket-Servers von Nico Kaiser so angepasst, dass sie auch den hybi10-Draft unterstützt.</p>
<p>Viel Spass bei basteln <img src='http://lemmingzshadow.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong style="text-decoration: underline;">HINWEIS:</strong> Da der Server aktuell ständig weiterentwickelt wird habe ich ein Repository auf Github angelegt. Bitte den jeweils aktuelle Code verwenden: <a href="https://github.com/lemmingzshadow/php-websocket">https://github.com/lemmingzshadow/php-websocket</a></p>
<p><strong style="text-decoration: underline;">UPDATE:</strong> As the project is constantly updated and improved I created a repository on github. Please get the code here: <a href="https://github.com/lemmingzshadow/php-websocket">https://github.com/lemmingzshadow/php-websocket</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lemmingzshadow.net/386/php-websocket-serverclient-nach-draft-hybi-10/feed/</wfw:commentRss>
		<slash:comments>36</slash:comments>
		</item>
		<item>
		<title>PHP Websocket Client</title>
		<link>http://lemmingzshadow.net/379/php-websocket-client/</link>
		<comments>http://lemmingzshadow.net/379/php-websocket-client/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 13:43:22 +0000</pubDate>
		<dc:creator>Lemmingz Shadow</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Websockets]]></category>

		<guid isPermaLink="false">http://lemmingzshadow.net/?p=379</guid>
		<description><![CDATA[Seit einiger Zeit spiele ich immer mal wieder mit Websockets. Allerdings habe ich bisher keinen wirklich funktionstüchtigen, in PHP implementierten Websocket-Client finden können. Also habe ich kurzerhand selbst eine Klasse dafür gebaut welche ich hier natürlich gerne veröffentliche. Absolut minimal, aber für Testzwecke ausreichend. Es wird aktuell der Handshake aus Draft &#8220;draft-ietf-hybi-thewebsocketprotocol-00&#8221; unterstürzt. Getestet habe [...]]]></description>
			<content:encoded><![CDATA[<p>Seit einiger Zeit spiele ich immer mal wieder mit <a href="http://lemmingzshadow.net/tag/websockets/">Websockets</a>. Allerdings habe ich bisher keinen wirklich funktionstüchtigen, in PHP implementierten Websocket-Client finden können. Also habe ich kurzerhand selbst eine Klasse dafür gebaut welche ich hier natürlich gerne veröffentliche. Absolut minimal, aber für Testzwecke ausreichend. Es wird aktuell der Handshake aus Draft &#8220;<a href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00">draft-ietf-hybi-thewebsocketprotocol-00</a>&#8221; unterstürzt. Getestet habe ich es mit der aktuellen Version von Chrome (13.0.782) und dem <a href="https://github.com/nicokaiser/php-websocket">Websocket Server von Nico Kaiser</a>.</p>
<p>Hier die Klasse + Beispielaufruf:</p>
<p><strong>ACHTUNG:</strong> Eine aktuellere Version dieser Klasse gibt es hier: <a href="http://lemmingzshadow.net/386/php-websocket-serverclient-nach-draft-hybi-10/">PHP Websocket Server/Client nach Draft hybi-10</a></p>
<p><span id="more-379"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Very basic websocket client.
 * Supporting handshake from drafts:
 *	draft-hixie-thewebsocketprotocol-76
 *	draft-ietf-hybi-thewebsocketprotocol-00
 * 
 * @author Simon Samtleben &lt;web@lemmingzshadow.net&gt;
 * @version 2011-09-15
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> WebsocketClient
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_Socket</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$port</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_connect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$port</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_disconnect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sendData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// send actual data:</span>
		<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_Socket<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\x00</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$data</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\xff</span>&quot;</span> <span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error:'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$errno</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">':'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$errstr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		<span style="color: #000088;">$wsData</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_Socket<span style="color: #339933;">,</span> <span style="color: #cc66cc;">2000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$retData</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wsData</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #660099; font-weight: bold;">\x00</span><span style="color: #660099; font-weight: bold;">\xff</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$retData</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _connect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$port</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$key1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_generateRandomString<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$key2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_generateRandomString<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$key3</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_generateRandomString<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
&nbsp;
		<span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GET /echo HTTP/1.1<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$header</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Upgrade: WebSocket<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$header</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Connection: Upgrade<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$header</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Host: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$host</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$port</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$header</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Origin: http://foobar.com<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$header</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Sec-WebSocket-Key1: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$key1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$header</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Sec-WebSocket-Key2: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$key2</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$header</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$header</span><span style="color: #339933;">.=</span> <span style="color: #000088;">$key3</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_Socket <span style="color: #339933;">=</span> <span style="color: #990000;">fsockopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$port</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errstr</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_Socket<span style="color: #339933;">,</span> <span style="color: #000088;">$header</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$errno</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">':'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$errstr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		<span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_Socket<span style="color: #339933;">,</span> <span style="color: #cc66cc;">2000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #009933; font-style: italic;">/**
		 * @todo: check response here. Currently not implemented cause &quot;2 key handshake&quot; is already deprecated.
		 * See: http://en.wikipedia.org/wiki/WebSocket#WebSocket_Protocol_Handshake
		 */</span>		
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _disconnect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_Socket<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _generateRandomString<span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #000088;">$addSpaces</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #000088;">$addNumbers</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>  
		<span style="color: #000088;">$characters</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!&quot;§$%&amp;/()=[]{}'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$useChars</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// select some random chars:    </span>
		<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$length</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$useChars</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$characters</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">mt_rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$characters</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #666666; font-style: italic;">// add spaces and numbers:</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$addSpaces</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$useChars</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$addNumbers</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$useChars</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #990000;">shuffle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$useChars</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$randomString</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$useChars</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$randomString</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$randomString</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$randomString</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$WebSocketClient</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WebsocketClient<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$WebSocketClient</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sendData</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'1337'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$WebSocketClient</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://lemmingzshadow.net/379/php-websocket-client/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Named subpatterns in PHP</title>
		<link>http://lemmingzshadow.net/369/named-subpatterns-in-php/</link>
		<comments>http://lemmingzshadow.net/369/named-subpatterns-in-php/#comments</comments>
		<pubDate>Thu, 12 May 2011 08:59:23 +0000</pubDate>
		<dc:creator>Lemmingz Shadow</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://lemmingzshadow.net/?p=369</guid>
		<description><![CDATA[Da ich in regulären Ausdrücken gerade häufiger sog. &#8220;benannte Teilmuster&#8221; (named subpatterns) nutze und es mir so vorkommt als würde diese ungemein hilfreiche &#8220;Funktion&#8221; eher selten benutzt, will ich hier kurz darauf hinweisen. Named subpatterns gibt es seit PHP 5.2.2 und sie funktionieren wie folgt: Angenommen ich möchte Daten aus verschiedenen Quellen verwenden, dann kann [...]]]></description>
			<content:encoded><![CDATA[<p>Da ich in regulären Ausdrücken gerade häufiger sog. &#8220;benannte Teilmuster&#8221; (named subpatterns) nutze und es mir so vorkommt als würde diese ungemein hilfreiche &#8220;Funktion&#8221; eher selten benutzt, will ich hier kurz darauf hinweisen. Named subpatterns gibt es seit PHP 5.2.2 und sie funktionieren wie folgt:</p>
<p>Angenommen ich möchte Daten aus verschiedenen Quellen verwenden, dann kann bzw. wird es vorkommen, dass diese Daten unterschiedliche Formate haben. Ein ganz simples Beispiel:</p>
<p>Quelle A: 44793 Bochum &#8211; Sonnig &#8211; 19 Grad<br />
Quelle B: 44793 Bochum &#8211; 19 Grad &#8211; Sonnig</p>
<p>Die gleichen Daten, aber ein andere Reihenfolge.<br />
Wenn ich nun für jede Datenquelle einen reguläre Ausdruck verwende um die Daten zu teilen, hatte man früher oft folgendes Problem:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;44793 Bochum - Sonnig - 19 Grad&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/(.+)\s-\s(.+)\s-\s(.+)/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$temp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><span id="more-369"></span><br />
Bei diesem Code finde ich die Temperatur einmal in $temp[3] und einmal in $temp[2]. Somit müsste ich für jede Datenquelle zusätzlich speichern in welchem Feld welcher Wert liegt. Und genau hier kommen die benannten Suchmuster ins Spiel. Durch eine kleine Modifikation des regulären Ausdrucks kann man den Teilmustern Namen geben:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;44793 Bochum - Sonnig - 19 Grad&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/(?&lt;ort&gt;.+)\s-\s(?&lt;descr&gt;.+)\s-\s(?&lt;temperatur&gt;.+)/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$temp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>ergibt dann:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Array
(
    [0] =&gt; 44793 Bochum - Sonnig - 19 Grad
    [ort] =&gt; 44793 Bochum
    [1] =&gt; 44793 Bochum
    [descr] =&gt; Sonnig
    [2] =&gt; Sonnig
    [temperatur] =&gt; 19 Grad
    [3] =&gt; 19 Grad
)</pre></div></div>

<p>Keine Magie, aber ungemein hilfreich wenn man es noch nicht kennt <img src='http://lemmingzshadow.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://lemmingzshadow.net/369/named-subpatterns-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mcrypt_create_iv teilweise sehr langsam</title>
		<link>http://lemmingzshadow.net/366/mcrypt_create_iv-teilweise-sehr-langsam/</link>
		<comments>http://lemmingzshadow.net/366/mcrypt_create_iv-teilweise-sehr-langsam/#comments</comments>
		<pubDate>Mon, 09 May 2011 11:19:54 +0000</pubDate>
		<dc:creator>Lemmingz Shadow</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://lemmingzshadow.net/?p=366</guid>
		<description><![CDATA[Für ein privates Projekt bastle ich seit einiger Zeit an einem IRC-Bot wobei folgendes Problem auftrat: Sobald Nachrichten in einem IRC-Channel gelesen oder geschrieben wurden hing das Script für einige Sekunden als würde irgendwo ein sleep ausgeführt. Dies war allerdings nicht immer der Fall und trat zudem nicht auf einem Windows System auf. (Weshalb mein [...]]]></description>
			<content:encoded><![CDATA[<p>Für ein privates Projekt bastle ich seit einiger Zeit an einem IRC-Bot wobei folgendes Problem auftrat:<br />
Sobald Nachrichten in einem IRC-Channel gelesen oder geschrieben wurden hing das Script für einige Sekunden als würde irgendwo ein sleep ausgeführt. Dies war allerdings nicht immer der Fall und trat zudem nicht auf einem Windows System auf. (Weshalb mein Xdebug-Profiling erfolglos war). Es blieb mir also nichts anderes übrig als solange Komponenten des Bots zu deaktivieren bis das Problem nicht mehr auftrat. So stelle ich fest dass, das Problem irgendwo in der Klasse für die FiSH-Verschlüsselung lag.<span id="more-366"></span><br />
Bei genauerer Analyse stelle sich heraus dass die Funktion mcrypt_create_iv aus der Mcrypt-Extension Ursache des Problems war. Diese Funktion generiert eine Initialisierungsvektor für die Ver-/Entschlüsselung und braucht dafür teilweise extrem lange. In meinem Fall 5-6 Sekunden. Eine Lösung für das Problem fand ich auf php.net: Statt MCRYPT_DEV_RANDOM kann man MCRYPT_DEV_URANDOM  als Quelle für die benötigen Zufallswerte benutzen und das Problem ist behoben. Ein simple Änderung mit großer Wirkung.</p>
<p>Vorher:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$iv</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_create_iv</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mcrypt_enc_get_iv_size</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$td</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> MCRYPT_DEV_RANDOM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Nacher:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$iv</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mcrypt_create_iv</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mcrypt_enc_get_iv_size</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$td</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> MCRYPT_DEV_URANDOM<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://lemmingzshadow.net/366/mcrypt_create_iv-teilweise-sehr-langsam/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Asynchrone PHP Prozesse mit Gearman</title>
		<link>http://lemmingzshadow.net/347/asynchrone-php-prozesse-mit-gearman/</link>
		<comments>http://lemmingzshadow.net/347/asynchrone-php-prozesse-mit-gearman/#comments</comments>
		<pubDate>Tue, 01 Mar 2011 16:03:28 +0000</pubDate>
		<dc:creator>Lemmingz Shadow</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Debian]]></category>
		<category><![CDATA[Gearman]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Webtechnik]]></category>

		<guid isPermaLink="false">http://lemmingzshadow.net/?p=347</guid>
		<description><![CDATA[Es kommt immer wieder vor, dass man aus einem PHP-Script heraus ein Anderes aufrufen möchte welches dann eine bestimmt Aufgabe erledigt. Dies ist z.B. sinnvoll wenn die Aufgabe länger dauert und man nicht im Script darauf warten möchte, wie etwa beim Erstellen von Bildern in verschiedenen Größen. Ich hatte bereits vor einiger Zeit über eine [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://lemmingzshadow.net/files/2011/03/gearman.gif" alt="Gearman" title="Gearman" width="80" height="80" style="float: left; padding: 5px;" /> Es kommt immer wieder vor, dass man aus einem PHP-Script heraus ein Anderes aufrufen möchte welches dann eine bestimmt Aufgabe erledigt. Dies ist z.B. sinnvoll wenn die Aufgabe länger dauert und man nicht im Script darauf warten möchte, wie etwa beim Erstellen von Bildern in verschiedenen Größen.<br />
Ich hatte bereits vor einiger Zeit über eine Möglichkeit berichtet <a href="http://lemmingzshadow.net/6/php-scripte-im-hintergrund-ausfuhren/">PHP-Script im Hintergrund auszuführen</a>.<br />
Diese Möglichkeit ist allerdings nicht wirklich schön und hat auch einige Nachteile. Z.B. kann man nur schwer abfragen wie der aktuelle Status des Scripts ist und auch die Verteilung auf verschiedene Server ist nicht trivial. Darum möchte ich hier eine wesentlich elegantere Möglichkeit vorstellen PHP-Prozesse asynchron laufen zu lassen. Und zwar mit Hilfe von <a href="http://gearman.org/">Gearman</a>:<span id="more-347"></span></p>
<p>Gearman ist ein Jobserver und ermöglicht es Aufgaben auf verschiedene Prozesse und/oder Maschinen auszulagern. Dabei arbeitet man mit sogenannten &#8220;Workern&#8221; die auf einem beliebigen Server darauf warten Aufgaben zu erledigen. Besonders schön ist, dass diese Worker ebenfalls in PHP programmiert werden können. Zunächst jedoch ein paar Worte zur Installation.</p>
<p>Auf einem Debian Squeeze System ist die Installation denkbar einfach. Einfach den Jobserver via Aptitude oder apt-get installieren. Das Paket heisst <a href="http://packages.debian.org/squeeze/gearman">&#8220;gearman&#8221;</a>. Im init.d Ordner liegt dann auch direkt ein Script mit dem der Server gestartet werden kann:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/etc/init.d/gearman-job-server start</pre></div></div>

<p>Um den Gearman Server in PHP &#8220;ansprechen&#8221; zu können benötigt man nun noch die <a href="http://de.php.net/manual/en/book.gearman.php">Gearman-Extension</a> welche über PECL installiert werden kann.<br />
Hinweis: Es wird das Paket &#8220;php5-dev&#8221; benötigt. Falls dieses noch nicht installiert ist sollte man das an dieser Stelle tun.</p>
<p>Dann die PHP-Extenstion über PECL installieren:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">pecl install gearman</pre></div></div>

<p>Hinweis: Falls das Paket hier nicht installiert werden kann weil es eine Beta Version ist, stattdessen folgenden Befehl verwenden:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">pecl install gearman-beta</pre></div></div>

<p>Damit sollte die Extension erstellt sein und kann in der php.ini geladen werden. Dazu einfache folgendes in der php.ini ergänzen:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">extension = gearman.so</pre></div></div>

<p>Hinweis: Da die Worker normalerweise als CLI-Programme laufen, die entsprechende php.ini nicht vergessen.</p>
<p>Nun sollte alles soweit sein, dass Gearman verwendet werden kann.</p>
<p>Zunächst brauchen wir einen Worker welcher im Hintergrund läuft und darauf wartet Aufgaben zu erledigen. Hier ein Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000000; font-weight: bold;">class</span> WorkerExample
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_GearmanWorker</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span>  __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_GearmanWorker <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GearmanWorker<span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_GearmanWorker<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addServer</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4730</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_startupWorker<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> _startupWorker<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_GearmanWorker<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addFunction</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'foobar'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'foobar'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_GearmanWorker<span style="color: #339933;">-&gt;</span><span style="color: #004000;">work</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> foobar<span style="color: #009900;">&#40;</span><span style="color: #000088;">$Job</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Job</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">workload</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// do some work here...</span>
		<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$Worker</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WorkerExample<span style="color: #339933;">;</span></pre></div></div>

<p>Diesen Worker kann man nun z.B. als worker.php speichern und dann zum testen in einem Screen laufen lassen:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">php worker.php</pre></div></div>

<p>Nun brauchen wir noch ein Script welches diesen Worker aufruft, der sogenannte Client:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$Client</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GearmanClient<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addServer</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4730</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">'foo'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">42</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'bar'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1337</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">doBackground</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'foobar'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Wenn nun dieser Client aufgerufen wird sollte man in dem Screen in dem der Worker läuft eine Ausgabe der übergebenen Parameter sehen.<br />
Im Code sieht man sofort dass die IP des Servers auf dem Gearman läuft angegeben wird. Hier kann natürlich auch ein anderer Server als localhost benutzt werden. Mit &#8220;doBackground&#8221; wird der Worker aufgerufen und das Script läuft direkt weiter ohne auf irgendeine Antwort des Workers zu warten.</p>
<p>Über die vielen weiteren Möglichkeiten des Gearman-Servers kann man sich am besten informieren wenn man einen Blick auf die Methoden der <a href="http://de3.php.net/manual/en/book.gearman.php">Gearman-Extension auf php.net</a> wirft. Und nun viel Spass beim experimentieren. <img src='http://lemmingzshadow.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://lemmingzshadow.net/347/asynchrone-php-prozesse-mit-gearman/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SFTP Upload mit PHP</title>
		<link>http://lemmingzshadow.net/301/sftp-upload-mit-php/</link>
		<comments>http://lemmingzshadow.net/301/sftp-upload-mit-php/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 19:49:27 +0000</pubDate>
		<dc:creator>Lemmingz Shadow</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SFTP]]></category>
		<category><![CDATA[Webtechnik]]></category>

		<guid isPermaLink="false">http://lemmingzshadow.net/?p=301</guid>
		<description><![CDATA[Seit einiger Zeit bietet PHP mit der SSH2-Library einige Methoden um das SFTP-Subsystem von SSH zu nutzen. Leider fehlt eine Methode um Dateien per SFTP hochzuladen. Zwar existiert mit der Methode ssh2_scp_send eine Möglichkeit Dateien über einer SSH-Verbindung hochzuladen, allerdings wird hier das SCP-Protokoll benutzt. Sollte der verwendete Account serverseitig jedoch auf SFTP beschränkt sein [...]]]></description>
			<content:encoded><![CDATA[<p>Seit einiger Zeit bietet PHP mit der <a href="http://de.php.net/manual/en/book.ssh2.php">SSH2-Library</a> einige Methoden um das SFTP-Subsystem von SSH zu nutzen. Leider fehlt eine Methode um Dateien per SFTP hochzuladen. Zwar existiert mit der Methode <em>ssh2_scp_send</em> eine Möglichkeit Dateien über einer SSH-Verbindung hochzuladen, allerdings wird hier das SCP-Protokoll benutzt. Sollte der verwendete Account serverseitig jedoch auf SFTP beschränkt sein funktioniert diese Methode nicht.<br />
Um dennoch Dateien per SFTP hochladen zu können kann man sich mit der Methode <em>fopen</em> und dem <a href="http://de.php.net/manual/en/wrappers.ssh2.php">ssh2-sftp Wrapper</a> eine funktionierende Lösung basteln. <span id="more-301"></span></p>
<p>Eine sftp-upload Funktion könnte beispielsweise so aussehen:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sftp_putfile<span style="color: #009900;">&#40;</span><span style="color: #000088;">$local_file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$remote_file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$mode</span> <span style="color: #339933;">=</span> <span style="color: #208080;">0664</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$remote_file</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$remote_file</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'/'</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$remote_file</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$remote_file</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$sftp_stream</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ssh2.sftp://'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sftp_connection</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$remote_file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$sftp_stream</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_sftp_error</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000088;">$data_to_send</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$local_file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data_to_send</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_sftp_error</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sftp_stream</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data_to_send</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_sftp_error</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sftp_stream</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Diese beispielhafte Methode stammt aus einer SFTP Klasse die ich vor kurzem für eine Projekt geschrieben habe. Die komplette Klasse habe ich <a href="http://lemmingzshadow.net/files/2010/08/class.sftp.php.txt">hier zum download</a> hinterlegt.</p>
]]></content:encoded>
			<wfw:commentRss>http://lemmingzshadow.net/301/sftp-upload-mit-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Twitter API mit oAuth nutzen</title>
		<link>http://lemmingzshadow.net/295/twitter-api-mit-oauth-nutzen/</link>
		<comments>http://lemmingzshadow.net/295/twitter-api-mit-oauth-nutzen/#comments</comments>
		<pubDate>Sun, 06 Jun 2010 11:58:48 +0000</pubDate>
		<dc:creator>Lemmingz Shadow</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[oAuth]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[Webtechnik]]></category>

		<guid isPermaLink="false">http://lemmingzshadow.net/?p=295</guid>
		<description><![CDATA[Am 30. Juni wird Twitter die Standard Authentifizierung der Twitter API abschalten. Bis dahin sollte man seine Scripte so umstellen, dass sie oAuth benutzen. Wie lange man dafür noch Zeit hat erfährt man übrigens hier: http://www.countdowntooauth.com/ Da es mittlerweile schon einige PHP Klassen gibt welche oAuth unterstützen ist die Umstellung relativ einfach zu bewerkstelligen. Hier [...]]]></description>
			<content:encoded><![CDATA[<p>Am 30. Juni wird Twitter die Standard Authentifizierung der Twitter API abschalten. Bis dahin sollte man seine Scripte so umstellen, dass sie oAuth benutzen. Wie lange man dafür noch Zeit hat erfährt man übrigens hier: <a href="http://www.countdowntooauth.com/">http://www.countdowntooauth.com/</a> <img src='http://lemmingzshadow.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Da es mittlerweile schon einige PHP Klassen gibt welche oAuth unterstützen ist die Umstellung relativ einfach zu bewerkstelligen. Hier ein kleines Tutorial:</p>
<p>Zunächst muss man auf <a href="http://dev.twitter.com/">http://dev.twitter.com/</a> eine neue &#8220;App&#8221; anlegen. (Vorher einloggen) Der Prozess ist selbsterklärend. Nachdem man die App angelegt hat erhält man einen &#8220;Consumer key&#8221; und ein &#8220;Consumer secret&#8221;. Diese beiden Strings benötigen wir später für die Authentifizierung. Weiterhin braucht man den &#8220;oAuth Token&#8221; und das &#8220;oAuth Token Secret&#8221;. Um diese zu bekommen oben auf <a href="http://dev.twitter.com/apps">Your apps</a> klicken. Dann auf &#8220;Edit Details&#8221; der entsprechen App. Nach zwei weiteren Klicks auf &#8220;Application detail&#8221; (rechts) und &#8220;My Access Token&#8221; (auch rechts) hat man alle benötigten Tokens und Passwörter zusammen die man in einem PHP-Script benötigt.<span id="more-295"></span></p>
<p>Nun kann man entweder selbst damit anfangen eine oAuth Authentifizierung zu coden oder man benutzt eine der fertigen Klassen. Ich habe für dieses Beispiel eine fertige <a href="http://github.com/abraham/twitteroauth">oAuth Klasse von Abraham Williams</a> benutzt.<br />
Hier ein kleines Codebeispiel für die Authentifizierung mit oAuth und dem anschließenden posten eines Tweets:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$consumer_key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'insert_key'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$consumer_secret</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'insert_secret'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$access_key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'insert_key'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$access_secret</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'insert_secret'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'twitteroauth/twitteroauth.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$twitter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TwitterOAuth <span style="color: #009900;">&#40;</span><span style="color: #000088;">$consumer_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$consumer_secret</span><span style="color: #339933;">,</span> <span style="color: #000088;">$access_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$access_secret</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$twitter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'statuses/update'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'status'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Testing oAuth. Please ignore...'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://lemmingzshadow.net/295/twitter-api-mit-oauth-nutzen/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Konstanten für Datumsformate in PHP</title>
		<link>http://lemmingzshadow.net/245/konstanten-fuer-datumsformate-in-php/</link>
		<comments>http://lemmingzshadow.net/245/konstanten-fuer-datumsformate-in-php/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 17:06:06 +0000</pubDate>
		<dc:creator>Lemmingz Shadow</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://lemmingzshadow.net/?p=245</guid>
		<description><![CDATA[Andauernd vergesse ich dass es Sie gibt: Konstanten für verschiedene Datumsformate. Ausserdem sind Sie (wie ich finde) bei php.net nur schlecht dokumentiert. Darum hier nun für alle ein Codebeispiel und der Link zu den restlichen Konstanten. Folgender Code würde z.B. soetwas ausgeben: Wednesday, 10-Feb-10 18:02:49 CET 1 echo date&#40;DATE_COOKIE, time&#40;&#41;&#41;; Alle dieser nützlichen Konstanten sind [...]]]></description>
			<content:encoded><![CDATA[<p>Andauernd vergesse ich dass es Sie gibt: Konstanten für verschiedene Datumsformate. Ausserdem sind Sie (wie ich finde) bei php.net nur schlecht dokumentiert. Darum hier nun für alle ein Codebeispiel und der Link zu den restlichen Konstanten.<br />
Folgender Code würde z.B. soetwas ausgeben:<br />
<em>Wednesday, 10-Feb-10 18:02:49 CET</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span>DATE_COOKIE<span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Alle dieser nützlichen Konstanten sind hier dokumentiert: <a href="http://php.net/manual/en/class.datetime.php"><strong>Datumskonstanten bei php.net</strong></a></p>
]]></content:encoded>
			<wfw:commentRss>http://lemmingzshadow.net/245/konstanten-fuer-datumsformate-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP: isset vs. array_key_exists</title>
		<link>http://lemmingzshadow.net/232/php-isset-vs-array_key_exists/</link>
		<comments>http://lemmingzshadow.net/232/php-isset-vs-array_key_exists/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 16:32:29 +0000</pubDate>
		<dc:creator>Lemmingz Shadow</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Webtechnik]]></category>

		<guid isPermaLink="false">http://lemmingzshadow.net/?p=232</guid>
		<description><![CDATA[Heute möchte ich mal ein paar Worte zu den Funktionen isset und array_key_exists loswerden. Genauer angeschaut habe ich mir das Verhalten der beiden Funktionen beim Prüfen ob ein Feld in einem Array existiert. Bevor ich zur Performance komme möchte ich jedoch noch auf ein paar Besonderheiten der Funktionen eingehen: Um zu prüfen ob ein Feld [...]]]></description>
			<content:encoded><![CDATA[<p>Heute möchte ich mal ein paar Worte zu den Funktionen <em>isset</em> und <em>array_key_exists</em> loswerden. Genauer angeschaut habe ich mir das Verhalten der beiden Funktionen beim Prüfen ob ein Feld in einem Array existiert. Bevor ich zur Performance komme möchte ich jedoch noch auf ein paar Besonderheiten der Funktionen eingehen:</p>
<p>Um zu prüfen ob ein Feld in einem Array existiert ohne dabei Fehlermeldungen zu erzeugen muss vor beiden Funktionen <em>isset</em> und <em>is_array</em> aufgerufen werden.</p>
<p><strong>Beispiel:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'bar'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$check</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'bar'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$foo</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$check</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-232"></span><br />
Der Grund hierfür ist nicht immer direkt ersichtlich, deswegen hier die Erklärung:<br />
<em>isset</em> erzeugt zwar keine Notices oder Warnings falls die Variable $foo nicht gesetzt ist, aber es gibt ein anderes Problem. Wenn die Variable $foo vom Typ String ist würde die Prüfung auf ein Array-Feld mit isset true liefern.</p>
<p><strong>Beispiel:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'bar'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'bar'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// true</span></pre></td></tr></table></div>

<p>Dieses Verhalten erklärt die Notwendigkeit von <em>is_array</em> vor dem Prüfen mit isset. Das <em>isset</em> vor dem <em>is_array</em> ist schließlich notwendig weil <em>is_array</em> eine Notice erzeugt wenn die zu prüfende Variable nicht gesetzt ist.</p>
<p>Die Funktion<em> array_key_exists</em> erzeugt ein Warning falls das zu prüfende Array nicht existiert oder die übergebene Variable nicht vom Typ Array ist. Somit ist hier ebenfalls die Nutzung von <em>is_array</em> in Kombination mit <em>isset</em> zu empfehlen.</p>
<p>Mit obiger Syntax lässt sich nun eigentlich in beide Fällen prüfen ob ein Feld im einem Array existiert. Dabei ist jedoch eine Ausnahme zu beachten, und zwar der Wert null in einem Array.</p>
<p><strong>Beispiel:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$foo</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'bar'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$foo</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'bar'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// false</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'bar'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$foo</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// true</span></pre></td></tr></table></div>

<p>Damit sollte die Verwendung der beiden Funktionen geklärt sein (falls nicht bitte ich um einen kurzen Kommentar), aber es bleibt die Frage: Welche der beiden Varianten ist schneller, <em>isset</em> oder <em>array_key_exists</em>?<br />
Hier das Ergebnis:<br />
<a href="http://lemmingzshadow.net/files/2009/10/performance_array_key_exists.jpg" rel="facebox"><img src="http://lemmingzshadow.net/files/2009/10/performance_array_key_exists-150x150.jpg" alt="performance_array_key_exists" title="performance_array_key_exists" width="150" height="150" class="alignnone size-thumbnail wp-image-240" style="float: left; margin: 5px;" /></a> <a href="http://lemmingzshadow.net/files/2009/10/performance_isset.jpg" rel="facebox"><img src="http://lemmingzshadow.net/files/2009/10/performance_isset-150x150.jpg" alt="performance_isset" title="performance_isset" width="150" height="150" class="alignnone size-thumbnail wp-image-241" style="float: left; margin: 5px;" /></a></p>
<div style="clear: both;"></div>
<p>Klare Sache: <em>isset</em> ist wesentlich schneller als <em>array_key_exists</em>, 10000 Prüfungen dauert nur etwa halb so lange.<br />
Zudem fallen zwei weitere Dinge auf: <em>isset</em> taucht nicht in den Xdebug Ergebnissen auf. Die Begründung ist relativ simpel. Da <em>isset</em> keine Funktion ist sondern lediglich ein Sprachkonstrukt, taucht dieses nicht in den Xdebug-Listings auf. Weiterhin fällt auf, dass is_array fast genauso viel Zeit in Anspruch nimmt wie <em>array_key_exists</em>. Hierfür habe ich leider keine Erklärung, denn ich hätte erwarten dass <em>array_key_exists</em> im Gegensatz zu <em>is_array</em> eine Array-Funktion und damit wesentlich langsamer ist. Falls jemand eine Erklärung hat: Immer her damit!</p>
]]></content:encoded>
			<wfw:commentRss>http://lemmingzshadow.net/232/php-isset-vs-array_key_exists/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP-Performance: preg_match vs. strpos</title>
		<link>http://lemmingzshadow.net/214/php-performance-preg_match-vs-strpos/</link>
		<comments>http://lemmingzshadow.net/214/php-performance-preg_match-vs-strpos/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 17:40:42 +0000</pubDate>
		<dc:creator>Lemmingz Shadow</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Webtechnik]]></category>

		<guid isPermaLink="false">http://lemmingzshadow.net/?p=214</guid>
		<description><![CDATA[Folgendes Problem hat man als Coder sicherlich öfters zu lösen: Man möchte wissen ob String A, String B oder String C (u.s.w) in einem Text vorkommen. Beispielsweise holt man eine Liste mit Keywords aus einer Datenbank und möchte wissen ob eins dieser Keywords in einem Text vorkommt. Um das Problem zu lösen gibt es sicherlich [...]]]></description>
			<content:encoded><![CDATA[<p>Folgendes Problem hat man als Coder sicherlich öfters zu lösen: Man möchte wissen ob String A,  String B oder String C (u.s.w) in einem Text vorkommen. Beispielsweise holt man eine Liste mit Keywords aus einer Datenbank und möchte wissen ob eins dieser Keywords in einem Text vorkommt.<br />
Um das Problem zu lösen gibt es sicherlich zig verschiedene Möglichkeiten. Die gängigsten sehen wahrscheinlich in etwas so aus:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">stripos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$found</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$needle</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'/is'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$haystack</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$found</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-214"></span><br />
Hierbei wird eine Sache schnell klar: Bei mehr als einem Suchsting wird das ganze etwas komplizierter. Man muss die strpos Variante für jeden Suchstring einmal laufen lassen, bzw. in der zweiten Variante die Suchstrings mit einem oder verknüpfen.<br />
Die Frage ist nun: Welche der beiden Varianten ist schneller?</p>
<p>Ich habe ein kurzes Testscript gebastelt, und hier ist das Ergebnis:<br />
<a href="http://lemmingzshadow.net/files/2009/10/performance_preg_match.jpg" rel="facebox"><img src="http://lemmingzshadow.net/files/2009/10/performance_preg_match-150x150.jpg" alt="performance_preg_match" title="performance_preg_match" width="150" height="150" class="alignnone size-thumbnail wp-image-217" style="margin: 5px; float: left;" /></a> <a href="http://lemmingzshadow.net/files/2009/10/performance_strpos.jpg" rel="facebox"><img src="http://lemmingzshadow.net/files/2009/10/performance_strpos-150x150.jpg" alt="performance_strpos" title="performance_strpos" width="150" height="150" class="alignnone size-thumbnail wp-image-228" style="margin: 5px; float: left;" /></a>
<div style="clear: both;"></div>
<p>Die preg_match Varinate ist um einiges schneller. Ausserdem fällt auf das bei 10000 Durchläufen des Testscript die Funktion stripos öfter als 10000 mal ausgeführt wird. Die liegt daran, dass die Funktion so oft aufgerufen werden muss, bis der erste Suchstring gefunden wurde (oder eben keiner der gesuchten Strings im Text vorkommt). Durch die oder-Verknüpfung der einzelnen Strings muss preg_match bei jedem Durchlauf nur einmal ausgeführt werden.<br />
Klar wird also: Je mehr Suchstrings in einem Text gesucht werden sollen, desto langsamer wird die strpos Variante im Vergleich zu preg_match, obwohl natürlich auch der reguläre Ausdruck bei einem längeren Suchstring etwas langsamer wird.</p>
<p>Zum Schluss noch das Script mit dem ich dieses Verhalten getestet habe:<br />
<a href='http://lemmingzshadow.net/files/2009/10/performance_preg_vs_strpos_php.txt'>performance_preg_vs_strpos.php</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lemmingzshadow.net/214/php-performance-preg_match-vs-strpos/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

