RestUtils using curl alternative

Last post 12-12-2007 7:51 PM by BarryR. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 12-04-2007 3:58 PM

    RestUtils using curl alternative

    Hello,

    I am looking for an alternative to curl, to add to the RestUtils.php from the file manager (for the benefit of php without curl enabled).

    I have been trying to use PEAR HTTP_REQUEST, but haven't got it working yet, it seems to get stuck on authentication. Is there an already-written alternative to the curl RestUtils? either using HTTP_REQUEST, fsockopen, or other?

    Thanks,

    Stan
     

    Filed under: , ,
  • 12-11-2007 11:52 PM In reply to

    • BarryR
    • Top 10 Contributor
    • Joined on 07-20-2007
    • San Diego
    • Posts 519

    Re: RestUtils using curl alternative

     We do not have any PEAR experts but fsockopen is fairly straight forward to use.  The following snippet was found on the PHP Documentation site:


    <?php
    $fp 
    fsockopen("www.example.com"80$errno$errstr30);
    if (!
    $fp) {
        echo 
    "$errstr ($errno)<br />\n";
    } else {
        
    $out "GET / HTTP/1.1\r\n";
        
    $out .= "Host: www.example.com\r\n";
        
    $out .= "Connection: Close\r\n\r\n";

        
    fwrite($fp$out);
        while (!
    feof($fp)) {
            echo 
    fgets($fp128);
        }
        
    fclose($fp);
    }
    ?>

     

    All you would need to do is change the site being referenced and any headers needed.  If you are having a specific problem please post it and we can try to get an answer to you.

    Regards,
         Barry R.

    IM Support (Feel free to add me)

    MSN: barryruffner@msn.com
    Gmail: barryruffner@gmail.com
    Filed under: , , ,
  • 12-12-2007 10:20 AM In reply to

    Re: RestUtils using curl alternative

    Hello,

    I have changed RestUtils to use pear HTTP_Request when no support for curl, just make sure php is compiled with openssl enabled, otherwise you can't authenticate with https.

    I actually didn't go through with checking that it's fully working, since it was just easier enabling curl for php in my case. But for the benefit of those who don't have curl:

    function doRequest($method, $url, $vars)
    {
        // use curl if exists
        if (function_exists('curl_init'))
        {
        // Init the CURL library
            $ch = curl_init();
            // Set the URL to request
            curl_setopt($ch, CURLOPT_URL, $url);
           
            // set the user agent based on the server settings.
            curl_setopt($ch, CURLOPT_USERAGENT, "PHP File System Manager");
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
           
            // This is to simplify release / distribution but should be changed
            // when you have a production PHP system to be enabled using good
            // certificates.
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
           
            // set the post values.
            if ($method == 'POST')
            {
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
            }
            // execute the curl request and return the xml response.
            $data = curl_exec($ch);
            if ($data)
            {
                return $data;
            }
            else
            {
                return curl_error($ch);
            }
            // clean up when finished with the curl library.
            curl_close($ch);
        }
        else // no curl, use HTTP_REQUEST
        {
            include_once "HTTP/Request.php";

            $request = new HTTP_Request($url,array('allowRedirects' => true, 'maxRedirects' => 10));
            if ($method == 'POST')
            {
                $request->setMethod(HTTP_REQUEST_METHOD_POST);
                if (is_array($vars))
                {
                    foreach ($vars as $key => $value)
                    {
                        $request->addPostData($key, $value, false);
                    }
                }
            }
            else
            {
                $request->setMethod(HTTP_REQUEST_METHOD_GET);
            }

            // add file if there is one
            if (is_array($vars))
            {
                foreach ($vars as $key => $value)
                {
                    if ($value[0]=="@")
                    {
                        $addFileRet = $request->addFile("file", substr($value,1));
                        if (PEAR::isError($addFileRet))
                        {
                            $result = $addFileRet->getMessage();
                            return $result;
                        }
                    }
                }
            }

            // send and receive
            if (!PEAR::isError($request->sendRequest()))
            {
                $result =  $request->getResponseBody();
            }

            return $result;
        }
    }



    Regards,

    Stan 

    Filed under: , ,
  • 12-12-2007 7:51 PM In reply to

    • BarryR
    • Top 10 Contributor
    • Joined on 07-20-2007
    • San Diego
    • Posts 519

    Re: RestUtils using curl alternative

    This will definitely help anyone using shared or non-dedicated environments.  When I have the chance I will test this out and make sure its in good working condition.

    Thank you,
         Barry R.

    IM Support (Feel free to add me)

    MSN: barryruffner@msn.com
    Gmail: barryruffner@gmail.com
    Filed under: , ,
Page 1 of 1 (4 items)