function testPostCurl()
 {
     $url = 'http://www.iana.org/';
     // host of example.org
     $method = 'POST';
     $headers = array();
     $postVals = array();
     $response = MendeleyUtil::runCurl($url, $method, $headers, $postVals);
     $this->assertTrue(!empty($response));
 }
Beispiel #2
0
 /**
  * Executes a CURL request
  *
  * @param $url string
  * 	URL to make request to
  * @param $method string
  * 	HTTP transfer method
  * @param $headers
  * 	HTTP transfer headers
  * @param $postvals
  * 	post values
  * @return string
  */
 public static function runCurl($url, $method = 'GET', $headers = array(), $postvals = null)
 {
     $ch = curl_init();
     curl_setopt($ch, CURLINFO_HEADER_OUT, true);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
     curl_setopt($ch, CURLOPT_HEADER, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_TIMEOUT, 3);
     curl_setopt($ch, CURLOPT_VERBOSE, true);
     switch ($method) {
         case 'POST':
             curl_setopt($ch, CURLOPT_POST, true);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $postvals);
             curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
             break;
         case 'DELETE':
             if (!empty($postvals)) {
                 $url = $url . '?' . $postvals;
             }
             break;
     }
     curl_setopt($ch, CURLOPT_URL, $url);
     $response = curl_exec($ch);
     $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     curl_close($ch);
     if ($text = MendeleyUtil::isError($http_code)) {
         if (Configuration::DEBUG > 0) {
             var_dump(compact('http_code', 'url', 'method'));
         }
         $response = json_decode($response);
         if (isset($response->error)) {
             $error = $response->error;
         } else {
             $error = 'unknown error';
         }
         throw new Exception(sprintf('Error %d (%s): %s', $http_code, $text, $error));
         $response = false;
     }
     return $response;
 }