當前位置: 首頁>>代碼示例>>PHP>>正文


PHP HTTP_Request2::sendRequest方法代碼示例

本文整理匯總了PHP中HTTP_Request2::sendRequest方法的典型用法代碼示例。如果您正苦於以下問題:PHP HTTP_Request2::sendRequest方法的具體用法?PHP HTTP_Request2::sendRequest怎麽用?PHP HTTP_Request2::sendRequest使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在HTTP_Request2的用法示例。


在下文中一共展示了HTTP_Request2::sendRequest方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: sendRequest

 /**
  * send a request
  *
  * @access   public
  * @param    string  uri to send data to
  * @param    string  body of the request
  * @param    array   headers for the request
  * @return   mixed   either
  */
 function sendRequest($url, $body, $headers)
 {
     $params = array('method' => 'POST', 'timeout' => 30);
     $request = new \HTTP_Request2($url);
     foreach ($headers as $name => $value) {
         $request->addHeader($name, $value);
     }
     $request->addRawPostData($body);
     $result = $request->sendRequest();
     if (PEAR::isError($result)) {
         throw new \Services\Ebay\Transport\Exception('Could not send request.');
     }
     $response = $request->getResponseBody();
     return $response;
 }
開發者ID:rtconner,項目名稱:services_ebay,代碼行數:24,代碼來源:HttpRequest.php

示例2: array


//.........這裏部分代碼省略.........
                   $err = PEAR::raiseError('HTTPS proxies are not supported.');
                   self::push_error($err);
                   return $err;
               }*/
             $params['http']['proxy'] = sprintf('tcp://%s:%d', $pxhost, $pxport);
         }
         $params['http']['header'] = '';
         foreach ($reqheads as $key => $value) {
             $params['http']['header'] .= sprintf("%s: %s\r\n", $key, $value);
         }
         $context = stream_context_create($params);
         // open a stream and send the request
         $fp = fopen($url, 'r', false, $context);
         if (!$fp) {
             $err = PEAR::raiseError(sprintf('Cannot connect to %s.', $url));
             self::push_error($err);
             return $err;
         }
         if ($outsec >= 0) {
             stream_set_timeout($fp, $outsec);
         }
         // process the response
         $meta_data = stream_get_meta_data($fp);
         if (strcasecmp($meta_data['wrapper_type'], 'cURL') == 0) {
             $errmsg = 'EstraierPure does not work with the cURL' . ' HTTP stream wrappers, please use PEAR::HTTP_Request2.';
             $err = PEAR::raiseError($errmsg);
             self::push_error($err);
             return $err;
         }
         if (!empty($meta_data['timed_out'])) {
             $err = PEAR::raiseError('Connection timed out.');
             self::push_error($err);
             return $err;
         }
         $first_header = array_shift($meta_data['wrapper_data']);
         if (!preg_match('!^HTTP/(.+?) (\\d+) ?(.*)!', $first_header, $matches)) {
             $err = PEAR::raiseError('Malformed response.');
             self::push_error($err);
             return $err;
         }
         $code = intval($matches[2]);
         if ($res instanceof EstraierPure_Response) {
             if ($res->save_heads) {
                 foreach ($meta_data['wrapper_data'] as $header) {
                     list($name, $value) = explode(':', $header, 2);
                     $res->add_head(strtolower($name), ltrim($value));
                 }
             }
             if ($res->save_body) {
                 $res->set_body(stream_get_contents($fp));
             }
         }
         // close the stream
         fclose($fp);
         // }}}
     } else {
         // {{{{ using PEAR::HTTP_Request2
         // set request parameters
         $params = array();
         $params['requestHeaders'] = $reqheads;
         if (isset($params['requestHeaders']['Content-Type'])) {
             unset($params['requestHeaders']['Content-Type']);
             $params['requestHeaders']['content-type'] = $reqheads['Content-Type'];
         }
         if (!is_null($pxhost)) {
             $params['proxy_host'] = $pxhost;
             $params['proxy_port'] = $pxport;
         }
         if ($outsec >= 0) {
             $params['timeout'] = floatval($outsec);
             $params['readTimeout'] = array($outsec, 0);
         }
         // create an instance of HTTP_Request2
         $req = new HTTP_Request2($url, $params);
         if (is_null($reqbody)) {
             $req->setMethod('GET');
         } else {
             $req->setMethod('POST');
             $req->setBody($reqbody);
         }
         // send the request
         $err = $req->sendRequest(is_object($res) && !empty($res->save_body));
         if (PEAR::isError($err)) {
             self::push_error($err);
             return $err;
         }
         $code = $req->getResponseCode();
         // process the response
         if ($res instanceof EstraierPure_Response) {
             if ($res->save_heads) {
                 $res->set_heads($req->getResponseHeader());
             }
             if ($res->save_body) {
                 $res->set_body($req->getResponseBody());
             }
         }
         // }}}
     }
     return $code;
 }
開發者ID:bermi,項目名稱:akelos,代碼行數:101,代碼來源:estraierpure.php


注:本文中的HTTP_Request2::sendRequest方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。