Curl Can';t通过查询字符串下载PISA数据

Curl Can';t通过查询字符串下载PISA数据,curl,bioinformatics,Curl,Bioinformatics,我正在尝试从下载文件。他们建议使用模板URL: http://www.ebi.ac.uk/pdbe/pisa/cgi-bin/interfaces.pisa?pdbcodelist 其中,pdbcodelist必须填写用户的首选项。例如: 到目前为止,我没有成功下载该文件。我曾尝试使用-L重定向,但没有任何运气 如果在web浏览器中键入示例URL,则会出现下载提示,允许您下载文件。我正试图用cURL完成同样的任务。URL确实会以某种方式指向一个可以下载的文件。但是,到目前为止,我还不知道如何

我正在尝试从下载文件。他们建议使用模板URL:

http://www.ebi.ac.uk/pdbe/pisa/cgi-bin/interfaces.pisa?pdbcodelist
其中,
pdbcodelist
必须填写用户的首选项。例如:

到目前为止,我没有成功下载该文件。我曾尝试使用
-L
重定向,但没有任何运气

如果在web浏览器中键入示例URL,则会出现下载提示,允许您下载文件。我正试图用cURL完成同样的任务。URL确实会以某种方式指向一个可以下载的文件。但是,到目前为止,我还不知道如何使用cURL来实现这一点。

当您使用cURL时

http://www.ebi.ac.uk/pdbe/pisa/cgi-bin/interfaces.pisa?4FGF
你得到这个标题

HTTP/1.1 302 Found
Server: Apache
Content-Type: text/html; charset=iso-8859-1
Date: Fri, 01 Feb 2013 19:29:11 GMT
Location: http://www.ebi.ac.uk/msd-srv/pisa/cgi-bin/interfaces.pisa?4FGF
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Length: 246
当您按照指定的位置操作时,您将在包含此内容的页面中结束

<pisa_interfaces>
  <status>Ok</status>
  <pdb_entry>
    <pdb_code>4FGF</pdb_code>
    <status>Entry not found</status>
  </pdb_entry>
</pisa_interfaces>

好啊
4FGF
找不到条目

您可以使用这个类,因为这个类的原始版本是在php.net上的

 <?php 
 function HeaderProc($response,$Run="",$String=1/*[Is 1 IF Use for String Mode ]*/){
          print_r($response);
          if($String==1){
             $response=explode("\r\n",$response);  
          }
          $PartHeader=0;
          $out[$PartHeader]=array();
          while(list($key,$val)=each($response)){
              $name='';
              $value='';
              $flag=false;
              for($i=0;$i<strlen($val);$i++){
                  if($val[$i]==":"){
                      $flag=true;
                      for($j=$i+1;$j<strlen($val);$j++){
                        if($val[$i]=="\r" and $val[$i+1]=="\n"){    
                            break;
                        }
                        $value.=$val[$j];
                      }
                      break;
                  }
                  $name.=$val[$i]; 
              }
              if($flag){
                if($name=='' and $value==''){
                    $PartHeader++;  
                }else{
                  if(isset($out[$PartHeader][$name])){
                    if(is_array($out[$PartHeader][$name])){   
                        $out[$PartHeader][$name][]=$value;
                    }else{
                        $T=$out[$PartHeader][$name];
                        $out[$PartHeader][$name]=array();
                        $out[$PartHeader][$name][0]=$T;  
                        $out[$PartHeader][$name][1]=$value;  
                    }
                  }else{
                    $out[$PartHeader][$name]=$value;
                  }
                }
              }else{
                if($name==''){
                    $PartHeader++;  
                }else{
                    if(isset($out[$PartHeader][$name])){ 
                      if(is_array($out[$PartHeader][$name])){   
                        $out[$PartHeader][$name][]=$value;
                      }else{
                        $T=$out[$PartHeader][$name];
                        $out[$PartHeader][$name]=array();
                        $out[$PartHeader][$name][0]=$T;  
                        $out[$PartHeader][$name][1]=$name;  
                      }
                    }else{
                        $out[$PartHeader][$name]=$name; 
                    }
                } 
              }
              if($Run!=""){
                $Run($name,$value);  
              }
          }
          return $out;
}
 class cURL { 
    var $headers; 
    var $user_agent; 
    var $compression; 
    var $cookie_file; 
    var $proxy; 
    var $Cookie; 
    function CookieAnalysis($Cookie){//convert str cookie to array cookie 
       //echo $Cookie;
       $this->Cookie=array();
       preg_replace_callback("~(.*?)=(.*?);~si",function($m){$this->Cookie[trim($m[1])]=trim($m[2]);},' '.$Cookie.'; ');
       return $this->Cookie;
    }
    function cURL($cookies=false,$cookie='cookies.txt',$compression='gzip',$proxy='') {
         $this->headers[] = 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
         $this->headers[] = 'Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3'; 
         $this->headers[] = 'Accept-Encoding:gzip,deflate,sdch';
         $this->headers[] = 'Accept-Language:en-US,en;q=0.8';
         $this->headers[] = 'Cache-Control:max-age=0';
         $this->headers[] = 'Connection:keep-alive';
         $this->user_agent = 'User-Agent:Mozilla/5.0 (SepidarSoft [Organic Search Engine Crawler] Linux Edition) AppleWebKit/536.5 (KHTML, like Gecko) SepidarBrowser/1.0.100.52 Safari/536.5';
         $this->compression=$compression; 
         $this->proxy=$proxy; 
         $this->cookies=$cookies; 
         if ($this->cookies == TRUE) $this->cookie($cookie); 
    } 
    function cookie($cookie_file) { 
         if (file_exists($cookie_file)) { 
            $this->cookie_file=$cookie_file; 
         } else { 
            fopen($cookie_file,'w') or $this->error('The cookie file could not be opened. Make sure this directory has the correct permissions');
            $this->cookie_file=$cookie_file; 
            @fclose($this->cookie_file); 
         } 
    }
    function GET($url) { 
         $process = curl_init($url); 
         curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers); 
         curl_setopt($process, CURLOPT_HEADER, 1); 
         curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent); 
         if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
         if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
         curl_setopt($process,CURLOPT_ENCODING , $this->compression); 
         curl_setopt($process, CURLOPT_TIMEOUT, 30); 
         if ($this->proxy) curl_setopt($process, CURLOPT_PROXY, $this->proxy); 
         curl_setopt($process, CURLOPT_RETURNTRANSFER, 1); 
         curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1); 
         $response = curl_exec($process);
         $header_size = curl_getinfo($process,CURLINFO_HEADER_SIZE);
         $result['Header'] = HeaderProc(substr($response, 0, $header_size),'',1);
         foreach($result['Header'] as $HeaderK=>$HeaderP){
           foreach($HeaderP['Set-Cookie'] as $key=>$val){
             $result['Header'][$HeaderK]['Set-Cookie'][$key]=$this->CookieAnalysis($val);
           }
         }
         $result['Body'] = substr( $response, $header_size );
         $result['HTTP_State'] = curl_getinfo($process,CURLINFO_HTTP_CODE);
         $result['URL'] = curl_getinfo($process,CURLINFO_EFFECTIVE_URL); 
         curl_close($process); 
         return $result; 
    }
    function POST($url,$data) { 
         $process = curl_init($url); 
         curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers); 
         curl_setopt($process, CURLOPT_HEADER, 1); 
         curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent); 
         if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
         if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
         curl_setopt($process, CURLOPT_ENCODING , $this->compression); 
         curl_setopt($process, CURLOPT_TIMEOUT, 30); 
         if ($this->proxy) curl_setopt($process, CURLOPT_PROXY, $this->proxy); 
         curl_setopt($process, CURLOPT_POSTFIELDS, $data); 
         curl_setopt($process, CURLOPT_RETURNTRANSFER, 1); 
         curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1); 
         curl_setopt($process, CURLOPT_POST, 1);
         $response = curl_exec($process); 
         $header_size = curl_getinfo($process,CURLINFO_HEADER_SIZE);
         $result['Header'] = HeaderProc(substr($response, 0, $header_size),'',1);
         foreach($result['Header'] as $HeaderK=>$HeaderP){
           foreach($HeaderP['Set-Cookie'] as $key=>$val){
             $result['Header'][$HeaderK]['Set-Cookie'][$key]=$this->CookieAnalysis($val);
           }
         }
         $result['Body'] = substr( $response, $header_size );
         $result['HTTP_State'] = curl_getinfo($process,CURLINFO_HTTP_CODE);
         $result['URL'] = curl_getinfo($process,CURLINFO_EFFECTIVE_URL);
         curl_close($process); 
         return $result; 
    }
    function error($error) { 
         echo "<center><div style='width:500px;border: 3px solid #FFEEFF; padding: 3px; background-color: #FFDDFF;font-family: verdana; font-size: 10px'><b>cURL Error</b><br>$error</div></center>";
         die; 
    } 
 } 

?>
对于do post请求,您可以使用blow代码

 $response=$cc->post('http://www.sepidarsoft.com','foo=bar');
 print_r($response);
或者对于DoGet请求,您可以使用blow代码

 $response=$cc->get('http://www.sepidarsoft.com');
 print_r($response);

此代码支持重定向

问题似乎只是您正在利用PDB代码。在上,所有示例代码都是小写。我猜驱动下载的脚本可能无法处理大写字母

当我在浏览器中输入URL时,会得到“Entry not found”XML,但使用小写可以解决这个问题

在我的机器上,以下各项完全按照预期工作:

$ curl -O http://www.ebi.ac.uk/msd-srv/pisa/cgi-bin/interfaces.pisa?4fgf

你的问题不清楚,这说明问题更清楚了吗?web浏览器可以使用相关URL查找要下载的文件。我需要使用cURL来下载该文件,因为我有一长串类似的文件要下载。如果你在web浏览器中键入该URL,你会得到一个内置下载提示。如果可能的话,我想使用cURL下载该文件。这可能吗?不幸的是,我无法理解此代码的作用,因此无法实现它。您是否能够从我提供的链接下载上述代码?此外,供您参考,实际上没有任何重定向正在进行。您请求的URL包含一个。这些字段被传递给CGI脚本(或类似脚本),该脚本生成包含所需内容的HTTP响应。您的浏览器设置将自动提示下载,但您可以使用cURL和其他工具来执行HTTP响应所需的操作。
$ curl -O http://www.ebi.ac.uk/msd-srv/pisa/cgi-bin/interfaces.pisa?4fgf