Are there any other options for rest clients besid

2019-05-31 03:40发布

问题:

Are there alternatives to CURL in PHP that will allow for a client to connect o a REST architecture server ?

PUT, DELETE, file upload are some of the things that need to work.

回答1:

You can write your own library. It's even possible to do it completely in PHP, using fsockopen and friends. For example:

function httpget($host, $uri) {
  $msg = 'GET '.$uri." HTTP/1.1\r\n".
         'Host: '.$host."\r\n".
         "Connection: close\r\n\r\n";
  $fh = fsockopen($host, 80);
  fwrite($fh, $msg);
  $result = '';
  while(!feof($fh)) {
    $result .= fgets($fh);
  }
  fclose($fh);
  return $result;
}


回答2:

I recommend Zend_Http_Client (from Zend) or HTTP_Request2 (from PEAR). They both provide a well-designed object model for making HTTP requests.

In my personal experience, I've found the Zend version to be a little more mature (mostly in dealing with edge cases).



标签: php rest