Example #1
0
 /**
  * @param string $sUrl
  * @param resource $rFile
  * @param string $sCustomUserAgent = 'MailSo Http User Agent (v1)'
  * @param string $sContentType = ''
  * @param int $iCode = 0
  * @param \MailSo\Log\Logger $oLogger = null
  * @param int $iTimeout = 10
  * @param string $sProxy = ''
  * @param string $sProxyAuth = ''
  * @param array $aHttpHeaders = array()
  * @param bool $bFollowLocation = true
  *
  * @return bool
  */
 public function SaveUrlToFile($sUrl, $rFile, $sCustomUserAgent = 'MailSo Http User Agent (v1)', &$sContentType = '', &$iCode = 0, $oLogger = null, $iTimeout = 10, $sProxy = '', $sProxyAuth = '', $aHttpHeaders = array(), $bFollowLocation = true)
 {
     if (null === $sCustomUserAgent) {
         $sCustomUserAgent = 'MailSo Http User Agent (v1)';
     }
     if (!is_resource($rFile)) {
         if ($oLogger) {
             $oLogger->Write('cURL: input resource invalid.', \MailSo\Log\Enumerations\Type::WARNING);
         }
         return false;
     }
     $aOptions = array(CURLOPT_URL => $sUrl, CURLOPT_HEADER => false, CURLOPT_FAILONERROR => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => !!$bFollowLocation, CURLOPT_MAXREDIRS => 7, CURLOPT_FILE => $rFile, CURLOPT_TIMEOUT => (int) $iTimeout);
     if (0 < \strlen($sCustomUserAgent)) {
         $aOptions[CURLOPT_USERAGENT] = $sCustomUserAgent;
     }
     if (0 < \strlen($sProxy)) {
         $aOptions[CURLOPT_PROXY] = $sProxy;
         if (0 < \strlen($sProxyAuth)) {
             $aOptions[CURLOPT_PROXYUSERPWD] = $sProxyAuth;
         }
     }
     if (\is_array($aHttpHeaders) && 0 < \count($aHttpHeaders)) {
         $aOptions[CURLOPT_HTTPHEADER] = $aHttpHeaders;
     }
     if ($oLogger) {
         $oLogger->Write('cUrl: URL: ' . $sUrl);
         if (isset($aOptions[CURLOPT_HTTPHEADER]) && \is_array($aOptions[CURLOPT_HTTPHEADER]) && 0 < \count($aOptions[CURLOPT_HTTPHEADER])) {
             $oLogger->Write('cUrl: Headers: ' . \print_r($aOptions[CURLOPT_HTTPHEADER], true));
         }
     }
     \MailSo\Base\Http::DetectAndHackFollowLocationUrl($sUrl, $aOptions, $oLogger);
     $oCurl = \curl_init();
     \curl_setopt_array($oCurl, $aOptions);
     $bResult = \curl_exec($oCurl);
     $iCode = (int) \curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
     $sContentType = (string) \curl_getinfo($oCurl, CURLINFO_CONTENT_TYPE);
     if ($oLogger) {
         $oLogger->Write('cUrl: Request result: ' . ($bResult ? 'true' : 'false') . ' (Status: ' . $iCode . ', ContentType: ' . $sContentType . ')');
         if (!$bResult || 200 !== $iCode) {
             $oLogger->Write('cUrl: Error: ' . \curl_error($oCurl), \MailSo\Log\Enumerations\Type::WARNING);
         }
     }
     if (\is_resource($oCurl)) {
         \curl_close($oCurl);
     }
     return $bResult;
 }