<?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>Georgi Mitev &#124; Learn Scripts Tutorials &#124; PHP Cron Mysql Flash &#187; ssl via php</title>
	<atom:link href="http://georgi-mitev.com/tag/ssl-via-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://georgi-mitev.com</link>
	<description>Georgi Mitev&#039;s blog for scripts tutorials and something else</description>
	<lastBuildDate>Thu, 29 Apr 2010 09:00:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>HTTP, HTTPS, and SSL via PHP</title>
		<link>http://georgi-mitev.com/2009/05/10/http-https-and-ssl-via-php/</link>
		<comments>http://georgi-mitev.com/2009/05/10/http-https-and-ssl-via-php/#comments</comments>
		<pubDate>Sun, 10 May 2009 07:08:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[https]]></category>
		<category><![CDATA[https and php]]></category>
		<category><![CDATA[https via php]]></category>
		<category><![CDATA[socket]]></category>
		<category><![CDATA[sockets]]></category>
		<category><![CDATA[ssl]]></category>
		<category><![CDATA[ssl and php]]></category>
		<category><![CDATA[ssl via php]]></category>

		<guid isPermaLink="false">http://georgi-mitev.com/?p=81</guid>
		<description><![CDATA[Today i found this very cool tutorial regarding the using of using ssl with PHP.
I&#8217;ll copy here the most interesting of it.
HTTP with PHP Sockets
// don&#8217;t need to specify http, it&#8217;s the default protocol
$hostname = &#8220;www.google.com&#8221;;
$port     = 80;
// create and configure the client socket
$fp = fsockopen($hostname, $port); // optional: $error_number, $error_string, [...]]]></description>
			<content:encoded><![CDATA[<p>Today i found this very cool tutorial regarding the using of using ssl with PHP.</p>
<p>I&#8217;ll copy here the most interesting of it.</p>
<h2><a name="http-sockets">HTTP with PHP Sockets</a></h2>
<p>// don&#8217;t need to specify http, it&#8217;s the default protocol<br />
$hostname = &#8220;www.google.com&#8221;;<br />
$port     = 80;</p>
<p>// create and configure the client socket<br />
$fp = fsockopen($hostname, $port); // optional: $error_number, $error_string, $connect_timeout<br />
if ($fp) {<br />
    stream_set_timeout($fp, 30); // seconds to wait for i/o operations</p>
<p>    // send request headers<br />
    fwrite($fp, &#8220;GET / HTTP/1.1\r\n&#8221;);<br />
    fwrite($fp, &#8220;Host: $hostname\r\n&#8221;);<br />
    fwrite($fp, $additional_headers); // Accept, User-Agent, Referer, etc.<br />
    fwrite($fp, &#8220;Connection: close\r\n&#8221;);</p>
<p>    // read response<br />
	$response = &#8220;&#8221;;<br />
    while (!feof($fp)) {<br />
        $response .= fgets($fp, 128);<br />
    }<br />
	echo $response;</p>
<p>    // close the socket<br />
    fclose($fp);<br />
}<br />
<span id="more-81"></span></p>
<h2><a name="raw-ssl-sockets">Direct SSL Sockets in PHP</a></h2>
<p>You don&#8217;t have to send request headers in the above example. You can actually connect to some servers and send raw text with <code>fwrite</code>. Because you have to implement the HTTP/HTTPS protocol yourself if you want it, sockets are considered &#8220;clean&#8221; connections.<br />
You don&#8217;t have to send request headers in the above example. You can actually connect to some servers and send raw text with fwrite. Because you have to implement the HTTP/HTTPS protocol yourself if you want it, sockets are considered &#8220;clean&#8221; connections.</p>
<p>// note that &#8220;ssl&#8221; is the protocol, NOT &#8220;https&#8221;<br />
$hostname  = &#8220;ssl://your-secure-site.com&#8221;;<br />
$port      = 443;</p>
<p>// create and configure the client socket<br />
$fp = fsockopen($hostname, $port);<br />
if ($fp) {<br />
    stream_set_timeout($fp, 30);</p>
<p>	// send data (or build HTTPS headers similar to first example)<br />
	fwrite($fp, &#8220;your message goes here&#8221;);</p>
<p>    // read response<br />
    while (!feof($fp)) {<br />
        echo fgets($fp, 128);<br />
    }</p>
<p>    // close the socket<br />
    fclose($fp);<br />
}</p>
<h2><a name="https-curl">HTTPS via cURL</a></h2>
<p>PHP doesn&#8217;t directly support attaching client certificates to web requests with <code>fsockopen</code>, so  you have to use the <code>cURL</code> library.<br />
$url = &#8220;https://your-secure-site.com/secure-service.php&#8221;;<br />
$port = 443;</p>
<p>// POST data, formatted just like a GET query string<br />
$request = &#8220;name=hb&amp;age=27&amp;site=arguments.callee.info&#8221;;</p>
<p>// the client certificate path MUST be a physical path, not url<br />
$certificate = &#8220;C:\\certificates\\test.crt&#8221;; // windows example<br />
$certificate = &#8220;/etc/pki/tls/private/test.crt&#8221;; // unix/linux example<br />
$password = &#8216;mypassword&#8217;; // client certificate&#8217;s key</p>
<p>// more details at http://php.net/curl<br />
$ch = curl_init();<br />
curl_setopt($ch, CURLOPT_POST, 1);<br />
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br />
curl_setopt($ch, CURLOPT_URL, $url);<br />
curl_setopt($ch, CURLOPT_PORT, $port);<br />
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);<br />
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);<br />
curl_setopt($ch, CURLOPT_SSLCERT, $certificate);<br />
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $password);</p>
<p>// fetch response and close the socket<br />
$response = curl_exec($ch);<br />
curl_close($ch);</p>
]]></content:encoded>
			<wfw:commentRss>http://georgi-mitev.com/2009/05/10/http-https-and-ssl-via-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
