代码之家  ›  专栏  ›  技术社区  ›  BrynJ

来自加拿大邮政的卷曲/屏幕刮取交付跟踪详细信息

  •  0
  • BrynJ  · 技术社区  · 15 年前

    我需要从加拿大邮政网站获取送货跟踪详细信息,该网站不提供API。

    
    class cURL { 
    var $headers; 
    var $user_agent; 
    var $compression; 
    var $cookie_file; 
    var $proxy; 
    function cURL($cookies=TRUE,$cookie='cookies.txt',$compression='gzip',$proxy='') { 
    $this->headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg'; 
    $this->headers[] = 'Connection: Keep-Alive'; 
    $this->headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8'; 
    $this->user_agent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)'; 
    $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, 0); 
    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($cUrl, CURLOPT_PROXY, 'proxy_ip:proxy_port'); 
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1); 
    $return = curl_exec($process); 
    curl_close($process); 
    return $return; 
    } 
    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); 
    $return = curl_exec($process); 
    curl_close($process); 
    return $return; 
    } 
    function error($error) { 
    echo "cURL Error
    $error"; die; } } $cc = new cURL(); $test = $cc->get('http://www.canadapost.ca/cpotools/apps/track/personal/findByTrackNumber?trackingNumber=x0x0x0x0x0x0x0&trackingType=trackPersonal'); echo $test;

    根据Tim的回复删除Accept标题行后,我现在看到一个页面,上面写着“您当前正在访问我们的基本网站”。此网站用于低带宽连接、移动设备和其他浏览器。”——但同样,没有跟踪信息。

    3 回复  |  直到 12 年前
        1
  •  1
  •   Tim Lytle    15 年前

    我认为问题在于这一行:

    $this->headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg'; 
    

    添加 text/html

        2
  •  1
  •   inakiabt    15 年前

    我曾经 Snoopy 用于屏幕擦伤。 完全推荐。

    更新: 我可以使用Snoopy获取该内容(但我需要修改一行:809)

    <?php
        include('Snoopy.class.php');
    
        $http = new Snoopy();
        $http->fetch('http://www.canadapost.ca/cpotools/apps/track/personal/findByTrackNumber?trackingNumber=x0x0x0x0x0x0x0&trackingType=trackPersonal');
    
        echo $http->results;
    ?>
    

    $cookie_headers .= $cookieKey."=".urlencode($cookieVal)."; ";
    

    与:

    $cookie_headers .= $cookieKey."=".$cookieVal."; ";
    

        3
  •  1
  •   Dss    15 年前

    这根线有多长时间了?Canadapost确实提供了一个API。 http://sellonline.canadapost.ca/DevelopersResources/