NYCPHP Meetup

NYPHP.org

[nycphp-talk] can i replace curl with sockets?

Hans Zaunere hans at nyphp.org
Wed Jan 29 16:21:58 EST 2003


--- Mark Armendariz <nyphp at enobrev.com> wrote:
> To start, I'm new to the list, so hello all.

Welcome.

> I'm working on a script that requires cURL, but I'd rather surpass the
> need for cURL as it isn't usually installed by default on servers
> (namely mine - but other servers I've been on hadn't had it installed
> either).

There's no absolute need for cURL, but considerable complexity is added
without it, especially since it looks like you need to speak HTTPS, and not
just HTTP.

> I realize I could try to use the command line cURL, but it's
> my understanding that that can be a security issue, and I would just
> like to handle it all within the script.

If you're careful, there shouldn't be any problems.  Try to limit user input
from getting into system() and related calls as much as possible, and you'll
be ok.

> I want to mimic this :
> ------------------------------
>  
>   $curl_holder = curl_init();
>   curl_setopt ($curl_holder , CURLOPT_URL, <https://k> '
> <https://www.url.com/script/> https://www.url.com/');
>   curl_setopt ($curl_holder , CURLOPT_HEADER, 0);
>   curl_setopt ($curl_holder , CURLOPT_POST, 1);
>   curl_setopt ($curl_holder , CURLOPT_POSTFIELDS, $vars);
>   curl_setopt ($curl_holder , CURLOPT_RETURNTRANSFER, 1);
> 
>   $server_response = curl_exec ($curl_holder );
>   curl_close ($curl_holder );
>  
>   print $server_response;
>  
> I've tried this:
> -----------------
>   
>    $vars = 'name=mark&user=enobrev'
>  
>    $header = 'POST /track HTTP/1.0' . "\\r\
";
>    $header .= 'Content-type: application/x-www-form-urlencoded' .
> "\\r\
";
>    $header .= 'Content-length: ' . strlen($vars) . "\\r\
\\r\
";
>  
>    $fp = fsockopen('www.url.com/script/', 443, $error_number,
> $error_sting);
>    
>    if ($fp) {
>      fputs($fp, $header . $vars);
>      
>     while (!feof($fp)) {
>       $response .= fgets($fp, 128);
>       fclose($fp);
>     }
>     
>     print htmlspecialchars($response);
>    } else {
>     print $error_number . ': ' . $error_sting . ' - could not connect to
> ' . $url);
>    }
>  
> And i get a whole bunch of these (which refers to the while line and the
> lines within the loop):
> --------------------
>  
> Warning: feof(): 2 is not a valid File-Handle resource

fsockopen() failed to open because 'www.url.com/script/' is not a valid
address (fsockopen() takes only a hostname [and protocol]).

$fp = fsockopen('www.url.com', 443, $error_number, $error_sting);

or

$fp = fsockopen('udp://www.url.com', 443, $error_number, $error_sting);

Plus, you're connecting to an encrypted service, which means you'll probably
have to have some handshaking done before you can begin to talk.  I don't
speak HTTPS, but it's probably not too fun.

> The Question
> -------------------
>  
> Am I going the right way about this, or is there a better way?

This is the right way; aside from using cURL :)  Or you could use real
sockets (as opposed to streams, of which fsockopen() is) if you have a lot of
time to kill.

http://www.php.net/manual/en/ref.sockets.php



=====
Hans Zaunere
President, New York PHP
http://nyphp.org
hans at nyphp.org



More information about the talk mailing list