0

这是我的 curl 函数,在此站点之前它运行良好:http ://www.finalpazarlama.com/kategoriler

 //Curl
function curl($site){
    $ch=curl_init();
    $maxredirect = 2;
    curl_setopt($ch, CURLOPT_URL, $site);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $mr = $maxredirect === null ? 5 : intval($maxredirect);
    if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')){
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
        curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
    }else{
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        if ($mr > 0){
            $newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
            $rch = curl_copy_handle($ch);
            curl_setopt($rch, CURLOPT_HEADER, true);
            curl_setopt($rch, CURLOPT_NOBODY, true);
            curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
            curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
            do {
                curl_setopt($rch, CURLOPT_URL, $newurl);
                $header = curl_exec($rch);
                if (curl_errno($rch)){
                    $code = 0;
                }else{
                    $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
                    if ($code == 301 || $code == 302){
                        preg_match('/Location:(.*?)\n/', $header, $matches);
                        $newurl = trim(array_pop($matches));
                    }else{
                        $code = 0;
                    }
                }
            }
            while ($code && --$mr);
            curl_close($rch);
            if (!$mr){
                if ($maxredirect === null){
                    trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.',E_USER_WARNING);
                }else{
                    $maxredirect = 0;
                }
                return false;
            }
            curl_setopt($ch, CURLOPT_URL, $newurl);
        }
    }
    return curl_exec($ch);
}

当我尝试使用http://www.finalpazarlama.com/kategoriler时,它返回空。可能是什么问题呢?为什么我不能得到它?

4

1 回答 1

0

HTTP/1.1 302 找到缓存控制:私有内容长度:157 内容类型:文本/html;charset=utf-8 位置:/PageNotFound?aspxerrorpath=/kategoriler 服务器:Microsoft-IIS/7.5 X-AspNetMvc-Version:4.0 X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET X-Powered- By-Plesk:PleskWin 日期:2013 年 9 月 15 日星期日 14:25:06 GMT

这就是响应,基本上你正在卷曲到一个 curl 不接受为有效的相对 url,即 /PageNotFound?aspxerrorpath=/kategoriler

您需要将 url 构建为绝对 url 我已经有一个函数:https://code.google.com/p/add-mvc-framework/source/browse/project/trunk/functions/url.functions。 php

随意复制这两个以得到如下结果:

<?php
//Curl
function curl($site){
    $ch=curl_init();
    $maxredirect = 2;
    curl_setopt($ch, CURLOPT_URL, $site);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $mr = $maxredirect === null ? 5 : intval($maxredirect);
    if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')){
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
        curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
    }else{
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
        if ($mr > 0){
            $newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
            $rch = curl_copy_handle($ch);
            curl_setopt($rch, CURLOPT_HEADER, true);
            curl_setopt($rch, CURLOPT_NOBODY, true);
            curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
            curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
            do {
                curl_setopt($rch, CURLOPT_URL, $newurl);
                $header = curl_exec($rch);
                if (curl_errno($rch)){
                    $code = 0;
                }else{
                    $code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
                    if ($code == 301 || $code == 302){
                        preg_match('/Location:(.*?)\n/', $header, $matches);
                        $newurl = trim(array_pop($matches));
                        $newurl = build_url($site,$newurl);
                    }else{
                        $code = 0;
                    }
                }
            }
            while ($code && --$mr);
            curl_close($rch);
            if (!$mr){
                if ($maxredirect === null){
                    trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.',E_USER_WARNING);
                }else{
                    $maxredirect = 0;
                }
                return false;
            }
            curl_setopt($ch, CURLOPT_URL, $newurl);
        }
    }
    return curl_exec($ch);
}

/**
 * URL functions
 *
 * @package ADD MVC\Functions
 *
 */

/**
 * Returns the complete url according to $base
 *
 * @param string $base
 * @param string $url
 *
 * @since ADD MVC 0.5
 *
 * @version 0.1
 */
function build_url($base,$url) {
   $base_parts=url_parts($base);

   # https://code.google.com/p/add-mvc-framework/issues/detail?id=81
   if (preg_match('/^(javascript|data)\:/',$url)) {
      return $url;
   }

   if ($url[0]==='/') {
      return rtrim($base_parts['protocol_domain'],'/').$url;
   }
   if ($url[0]==='?') {
      if (!$base_parts['pathname'])
         $base_parts['pathname']='/';
      return $base_parts['protocol_domain'].$base_parts['pathname'].$url;
   }
   if ($url[0]==='#') {

   }
   if (preg_match('/^https?\:\/+/',$url)) {
      return $url;
   }

   return rtrim($base_parts['protocol_domain'],"/").$base_parts['path'].$url;
}

/**
 * Returns the URL parts of the url
 *
 * @param string $url
 *
 * @since ADD MVC 0.5
 */
function url_parts($url) {
   if (!preg_match('/^(?P<protocol_domain>(?P<protocol>https?\:\/+)(?P<domain>([^\/\W]|[\.\-])+))(?P<request_uri>(?P<pathname>(?P<path>\/(.+\/)?)?(?P<file>[^\?\#]+?)?)?(?P<query_string>\?[^\#]*)?)(\#(?P<hash>.*))?$/',$url,$url_parts)) {
      echo debug_backtrace();
      throw new Exception("Invalid url: $url");
   }
   return $url_parts;
}
echo "<xmp>";
var_dump(curl('http://www.finalpazarlama.com/kategoriler'));
于 2013-09-15T14:38:16.690 回答