public function sendRequest(RequestInterface $request, ProxyInterface $proxy = null, CookieJarInterface $cookieJar = null)
 {
     $commandOptions = $this->phantomJsOptions;
     if ($proxy) {
         $proxyHost = $proxy->getHost() . ':' . $proxy->getPort();
         $commandOptions[] = '--proxy=' . $proxyHost;
         if ($user = $proxy->getUser()) {
             $proxyAuth = $user;
             if ($password = $proxy->getPassword()) {
                 $proxyAuth .= ':' . $password;
             }
             $commandOptions[] = '--proxy-auth=' . $proxyAuth;
         }
         if ($proxy->getType() == 'SOCKS5') {
             $commandOptions[] = '--proxy-type=socks5';
         } elseif ($proxy->getType() == 'SOCKS4') {
             throw new Exception('SOCKS4 proxy are not supported by phantomjs');
         }
     }
     $initialUrl = (string) $request->getUri();
     $commandArg = ['method' => $request->getMethod(), 'url' => $initialUrl, 'headers' => []];
     foreach ($request->getHeaders() as $headerName => $headerValues) {
         $commandArg['headers'][$headerName] = implode(',', $headerValues);
     }
     $data = (string) $request->getBody();
     if ($data) {
         $commandArg['data'] = $data;
     }
     $scriptFile = __DIR__ . '/phantomjs.js';
     $commandOptions = implode(' ', $commandOptions);
     $commandArg = json_encode($commandArg);
     $process = new Process("{$this->phantomJS} {$commandOptions} {$scriptFile} '{$commandArg}'");
     $process->run();
     if (!$process->isSuccessful()) {
         throw new ProcessFailedException($process);
     }
     $dataResponse = json_decode($process->getOutput(), true);
     if (!$dataResponse) {
         throw new Exception('Unable to parse Phantomjs response: ' . json_last_error_msg());
     }
     $response = new SearchEngineResponse($dataResponse['headers'], $dataResponse['status'], $dataResponse['content'], true, UrlArchive::fromString($initialUrl), UrlArchive::fromString($dataResponse['url']), $proxy);
     return $response;
 }
Exemplo n.º 2
0
 /**
  * @inheritdoc
  */
 public function sendRequest(RequestInterface $request, ProxyInterface $proxy = null, CookieJarInterface $cookieJar = null)
 {
     if ($proxy) {
         $proxyHost = $proxy->getHost();
         $proxyPort = $proxy->getPort();
         $proxyType = $proxy->getType();
         switch ($proxyType) {
             case 'SOCKS4':
                 $this->client->setOption(CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
                 break;
             case 'SOCKS5':
                 $this->client->setOption(CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
                 break;
             default:
                 $this->client->setOption(CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
                 break;
         }
         if ($user = $proxy->getUser()) {
             $proxyAuth = $user;
             if ($password = $proxy->getPassword()) {
                 $proxyAuth .= ':' . $password;
             }
         } else {
             $proxyAuth = null;
         }
         $this->client->setOption(CURLOPT_PROXY, $proxyHost);
         $this->client->setOption(CURLOPT_PROXYPORT, $proxyPort);
         if ($proxyAuth) {
             $this->client->setOption(CURLOPT_PROXYUSERPWD, $proxyAuth);
         } else {
             $this->client->removeOption(CURLOPT_PROXYUSERPWD);
         }
     } else {
         $this->client->removeOption(CURLOPT_PROXY);
         $this->client->removeOption(CURLOPT_PROXYPORT);
         $this->client->removeOption(CURLOPT_PROXYUSERPWD);
     }
     if ($cookieJar) {
         $cookieFileData = CookieFile::generate($cookieJar->all());
         $cookieFile = tempnam(sys_get_temp_dir(), 'serps_curlcookie');
         file_put_contents($cookieFile, $cookieFileData);
         $cookieJarFile = tempnam(sys_get_temp_dir(), 'serps_curlcookiejar');
         $this->client->setOption(CURLOPT_COOKIEFILE, $cookieFile);
         $this->client->setOption(CURLOPT_COOKIEJAR, $cookieJarFile);
     } else {
         $this->client->removeOption(CURLOPT_COOKIEFILE);
         $this->client->removeOption(CURLOPT_COOKIEJAR);
     }
     try {
         $rawResponse = $this->client->request($request);
         $headerSize = $this->client->getInfo(CURLINFO_HEADER_SIZE);
         $effectiveUrl = UrlArchive::fromString($this->client->getInfo(CURLINFO_EFFECTIVE_URL));
         $initialUrl = UrlArchive::fromString((string) $request->getUri());
         $this->client->close();
         if ($cookieJar) {
             $cookieJarData = file_get_contents($cookieJarFile);
             $cookies = CookieFile::parse($cookieJarData);
             foreach ($cookies as $cookie) {
                 $cookieJar->set($cookie);
             }
         }
         return ResponseBuilder::buildResponse($rawResponse, $headerSize, $initialUrl, $effectiveUrl, $proxy);
     } catch (\Exception $e) {
         throw $e;
     } finally {
         if ($cookieJar) {
             unlink($cookieFile);
             unlink($cookieJarFile);
         }
     }
 }