private function sendRequest($url, $method, $headers = null, $body = null)
 {
     $this->curl->headers = array();
     $this->curl->options = array();
     $this->curl->options['CURLOPT_SSL_VERIFYHOST'] = false;
     $this->curl->options['CURLOPT_SSL_VERIFYPEER'] = false;
     $this->curl->headers['Accept'] = VMware_VCloud_SDK_Constants::VCLOUD_ACCEPT_HEADER . ';' . 'version=' . $this->apiVersion;
     if ($this->authToken) {
         $this->curl->headers[VMware_VCloud_SDK_Constants::VCLOUD_AUTH_TOKEN] = $this->authToken;
     } else {
         $this->curl->options['CURLOPT_USERPWD'] = $this->username . ':' . $this->password;
     }
     if ($headers) {
         $h = $this->curl->headers;
         $h = array_merge($h, $headers);
         $this->curl->headers = $h;
     }
     $response = $this->curl->{$method}($url, $body);
     //  var_dump($url,$method,$this->curl,$body);
     if (!is_object($response) && $this->curl->error()) {
         // var_dump($url,$method,$headers,$body,$this->curl);
         throw new Exception($this->curl->error());
     }
     if (!$this->authToken) {
         $this->authToken = $response->headers[VMware_VCloud_SDK_Constants::VCLOUD_AUTH_TOKEN];
     }
     return $response;
 }
Example #2
0
 function testEnsureGettingErrorReturnsCorrectInfo()
 {
     $curl = new Curl(self::TEST_VALID_URL);
     $this->assertEquals(0, $curl->errno());
     $this->assertEquals('', $curl->error());
     $curl->setopt(CURLOPT_RETURNTRANSFER, 1);
     $curl->setopt(CURLOPT_PROXY, $this->proxy);
     $curl->exec();
     $this->assertEquals(0, $curl->errno());
     $this->assertEquals('', $curl->error());
 }
Example #3
0
 function testError()
 {
     $curl = new Curl();
     $curl->get('diaewkaksdljf-invalid-url-dot-com.com');
     $err = $curl->error();
     $this->assertTrue(!empty($err));
 }
 public static function getProblemInfo($problemId)
 {
     // 新建一个curl
     $ch = new Curl();
     $url = 'http://poj.org/problem?id=' . $problemId;
     $html = $ch->get($url);
     if (empty($html) || $ch->error()) {
         $ch->close();
         return false;
     }
     $ch->close();
     $problemInfo = array();
     $matches = array();
     // 获取标题
     preg_match('/<div class="ptt" lang="en-US">(.*)<\\/div>/sU', $html, $matches);
     $problemInfo['title'] = '';
     if (!empty($matches[1])) {
         $problemInfo['title'] = trim($matches[1]);
     }
     // 获取来源
     preg_match('/<a href="searchproblem\\?field=source.*>(.*)<\\/a>/sU', $html, $matches);
     $problemInfo['source'] = '';
     if (!empty($matches[1])) {
         $problemInfo['source'] = trim($matches[1]);
     }
     $problemInfo['problem_id'] = $problemId;
     $problemInfo['problem_code'] = $problemId;
     return $problemInfo;
 }
 public static function getProblemInfo($problemId)
 {
     // 新建一个curl
     $ch = new Curl();
     $url = 'http://acm.hdu.edu.cn/showproblem.php?pid=' . $problemId;
     $html = $ch->get($url);
     if (empty($html) || $ch->error()) {
         $ch->close();
         return false;
     }
     $ch->close();
     $problemInfo = array();
     $matches = array();
     // 获取标题
     preg_match('/<td align=center><h1 style=\'color:#1A5CC8\'>(.*)<\\/h1>/sU', $html, $matches);
     $problemInfo['title'] = '';
     if (!empty($matches[1])) {
         $problemInfo['title'] = trim($matches[1]);
         $problemInfo['title'] = iconv('GBK', 'UTF-8', $problemInfo['title']);
     }
     // 获取来源
     preg_match('/>Source.*<a.*\\/search.php.*>(.*)<\\/a>/sU', $html, $matches);
     $problemInfo['source'] = '';
     if (!empty($matches[1])) {
         $problemInfo['source'] = trim($matches[1]);
         $problemInfo['source'] = iconv('GBK', 'UTF-8', $problemInfo['source']);
     }
     $problemInfo['problem_id'] = $problemId;
     $problemInfo['problem_code'] = $problemId;
     return $problemInfo;
 }
Example #6
0
 public function get($url_mixed, $data = array())
 {
     if (is_array($url_mixed)) {
         $curl_multi = curl_multi_init();
         $this->multi_parent = true;
         $this->curls = array();
         foreach ($url_mixed as $url) {
             $curl = new Curl();
             $curl->multi_child = true;
             $curl->beforeSend($this->before_send_function);
             $curl->success($this->success_function);
             $curl->error($this->error_function);
             $curl->complete($this->complete_function);
             $curl->base_url = $url;
             $curl->url = $this->buildURL($url, $data);
             $curl->setOpt(CURLOPT_URL, $curl->url, $curl->curl);
             $curl->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
             $curl->setOpt(CURLOPT_HTTPGET, true);
             $this->curls[] = $curl;
             $curlm_error_code = curl_multi_add_handle($curl_multi, $curl->curl);
             if (!($curlm_error_code === CURLM_OK)) {
                 throw new \ErrorException('cURL multi add handle error: ' . curl_multi_strerror($curlm_error_code));
             }
         }
         foreach ($this->curls as $ch) {
             foreach ($this->options as $key => $value) {
                 $ch->setOpt($key, $value);
             }
         }
         do {
             curl_multi_select($curl_multi);
             $status = curl_multi_exec($curl_multi, $active);
         } while ($status === CURLM_CALL_MULTI_PERFORM || $active);
         while (!($info_array = curl_multi_info_read($curl_multi)) === false) {
             if (!($info_array['msg'] === CURLMSG_DONE)) {
                 continue;
             }
             foreach ($this->curls as $ch) {
                 if ($ch->curl === $info_array['handle']) {
                     $ch->curl_error_code = $info_array['result'];
                     break;
                 }
             }
         }
         foreach ($this->curls as $ch) {
             $this->exec($ch);
         }
     } else {
         $this->base_url = $url_mixed;
         $this->url = $this->buildURL($url_mixed, $data);
         $this->setOpt(CURLOPT_URL, $this->url);
         $this->setOpt(CURLOPT_CUSTOMREQUEST, 'GET');
         $this->setOpt(CURLOPT_HTTPGET, true);
         return $this->exec();
     }
 }
 public static function getProblemInfo($problemCode)
 {
     // 新建一个curl
     $ch = new Curl();
     $url = 'http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=' . $problemCode;
     $html = $ch->get($url);
     if (empty($html) || $ch->error()) {
         $ch->close();
         return false;
     }
     $ch->close();
     $problemInfo = array();
     $matches = array();
     // 获取标题
     preg_match('/<span class="bigProblemTitle">(.*)<\\/span>/sU', $html, $matches);
     $problemInfo['title'] = '';
     if (!empty($matches[1])) {
         $problemInfo['title'] = trim($matches[1]);
     }
     // 获取来源
     preg_match('/Source:.*<strong>(.*)<\\/strong>/sU', $html, $matches);
     if (empty($matches[1])) {
         preg_match('/Contest:.*<strong>(.*)<\\/strong>/sU', $html, $matches);
     }
     $problemInfo['source'] = '';
     if (!empty($matches[1])) {
         $problemInfo['source'] = trim($matches[1]);
     }
     // 获取$problemId
     preg_match('/"\\/onlinejudge\\/submit.do\\?problemId=(\\d+)"/sU', $html, $matches);
     $problemInfo['problem_id'] = 0;
     if (!empty($matches[1])) {
         $problemInfo['problem_id'] = $matches[1];
     }
     $problemInfo['problem_code'] = $problemCode;
     $problemInfo['remote'] = StatusVars::REMOTE_ZOJ;
     return $problemInfo;
 }
Example #8
0
 public function testErrorCallback()
 {
     $success_called = FALSE;
     $error_called = FALSE;
     $complete_called = FALSE;
     $curl = new Curl();
     $curl->setHeader('X-DEBUG-TEST', 'get');
     $curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE);
     $curl->setOpt(CURLOPT_SSL_VERIFYHOST, FALSE);
     $curl->setOpt(CURLOPT_CONNECTTIMEOUT_MS, 2000);
     $curl->success(function ($instance) use(&$success_called, &$error_called, &$complete_called) {
         PHPUnit_Framework_Assert::assertInstanceOf('Curl', $instance);
         PHPUnit_Framework_Assert::assertFalse($success_called);
         PHPUnit_Framework_Assert::assertFalse($error_called);
         PHPUnit_Framework_Assert::assertFalse($complete_called);
         $success_called = TRUE;
     });
     $curl->error(function ($instance) use(&$success_called, &$error_called, &$complete_called, &$curl) {
         PHPUnit_Framework_Assert::assertInstanceOf('Curl', $instance);
         PHPUnit_Framework_Assert::assertFalse($success_called);
         PHPUnit_Framework_Assert::assertFalse($error_called);
         PHPUnit_Framework_Assert::assertFalse($complete_called);
         $error_called = TRUE;
     });
     $curl->complete(function ($instance) use(&$success_called, &$error_called, &$complete_called) {
         PHPUnit_Framework_Assert::assertInstanceOf('Curl', $instance);
         PHPUnit_Framework_Assert::assertFalse($success_called);
         PHPUnit_Framework_Assert::assertTrue($error_called);
         PHPUnit_Framework_Assert::assertFalse($complete_called);
         $complete_called = TRUE;
     });
     $curl->get(Test::ERROR_URL);
     $this->assertFalse($success_called);
     $this->assertTrue($error_called);
     $this->assertTrue($complete_called);
 }
Example #9
0
    }
}
if (is_null($length)) {
    die_and_log($status = 400, "unknown content size:", $url);
}
if ($length > MAX_SIZE) {
    die_and_log($status = 400, "content size over: " . MAX_SIZE . "<{$length}bytes", $url);
}
if (!preg_match("/\\Aimage\\/(png|jpeg|gif)\\z/ui", $content_type)) {
    die_and_log($status = 400, "not support content type:" . $content_type, $url);
}
// get access
$c = new Curl();
$c->setHeader('X-Forwarded-For', getRemoteIP());
$c->error(function () {
    die_and_log($status = 500, "get request error");
});
$c->get($url);
// dump
header("Content-Type: {$content_type}");
echo $c->response;
//--functions--
function die_and_log($status = 400, $str = '', $extra = '')
{
    header("HTTP/1.1 {$status} Error");
    echo $str;
    error_log("{$str} :: {$extra}");
    exit;
}
function getRemoteIP()
{