cosname

cURL and fputs fallback POST reuqest example function

Jun 7th, 2014
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.09 KB | None | 0 0
  1. function post ($data, $type = null) {
  2.     if ( is_null($type) ) {
  3.         $url = $this->rest_endpoint;
  4.     }
  5.    
  6.     if ( !is_null($this->custom_post) ) {
  7.         return call_user_func($this->custom_post, $url, $data);
  8.     }
  9.    
  10.     if ( !preg_match("|http://(.*?)(/.*)|", $url, $matches) ) {
  11.         die('There was some problem figuring out your endpoint');
  12.     }
  13.    
  14.     if ( function_exists('curl_init') ) {
  15.         // Has curl. Use it!
  16.         $curl = curl_init($this->rest_endpoint);
  17.         curl_setopt($curl, CURLOPT_POST, true);
  18.         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  19.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  20.         $response = curl_exec($curl);
  21.         curl_close($curl);
  22.     } else {
  23.         // Use sockets.
  24.         foreach ( $data as $key => $value ) {
  25.             $data[$key] = $key . '=' . urlencode($value);
  26.         }
  27.         $data = implode('&', $data);
  28.    
  29.         $fp = @pfsockopen($matches[1], 80);
  30.         if (!$fp) {
  31.             die('Could not connect to the web service');
  32.         }
  33.         fputs ($fp,'POST ' . $matches[2] . " HTTP/1.1\n");
  34.         fputs ($fp,'Host: ' . $matches[1] . "\n");
  35.         fputs ($fp,"Content-type: application/x-www-form-urlencoded\n");
  36.         fputs ($fp,"Content-length: ".strlen($data)."\n");
  37.         fputs ($fp,"Connection: close\r\n\r\n");
  38.         fputs ($fp,$data . "\n\n");
  39.         $response = "";
  40.         while(!feof($fp)) {
  41.             $response .= fgets($fp, 1024);
  42.         }
  43.         fclose ($fp);
  44.         $chunked = false;
  45.         $http_status = trim(substr($response, 0, strpos($response, "\n")));
  46.         if ( $http_status != 'HTTP/1.1 200 OK' ) {
  47.             die('The web service endpoint returned a "' . $http_status . '" response');
  48.         }
  49.         if ( strpos($response, 'Transfer-Encoding: chunked') !== false ) {
  50.             $temp = trim(strstr($response, "\r\n\r\n"));
  51.             $response = '';
  52.             $length = trim(substr($temp, 0, strpos($temp, "\r")));
  53.             while ( trim($temp) != "0" && ($length = trim(substr($temp, 0, strpos($temp, "\r")))) != "0" ) {
  54.                 $response .= trim(substr($temp, strlen($length)+2, hexdec($length)));
  55.                 $temp = trim(substr($temp, strlen($length) + 2 + hexdec($length)));
  56.             }
  57.         } elseif ( strpos($response, 'HTTP/1.1 200 OK') !== false ) {
  58.             $response = trim(strstr($response, "\r\n\r\n"));
  59.         }
  60.     }
  61.     return $response;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment