代码之家  ›  专栏  ›  技术社区  ›  Christian Gallarmin

我怎样才能爬?

  •  2
  • Christian Gallarmin  · 技术社区  · 8 年前

    我的深度爬网只提供空值。

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL,"https://api.deepcrawl.com/accounts/00000/projects/195334/crawls/1306396/reports/thin_pages_basic");
    
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Auth-Token:Private'));
    
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $server_output2 = curl_exec ($ch);
    curl_close ($ch); 
    
    $results = json_decode($server_output2);
    

    curl -X POST -u '123:abcdef' https://api.deepcrawl.com/sessions
    {
    "token":"abcdef123",
    "_user_href":"/users/example-user",
    ...
    }
    

    然后,从会话调用返回的令牌作为X-Auth-token头传递给所有API调用:

    curl -X GET -H 'X-Auth-Token:' https://api.deepcrawl.com/accounts/1/projects
    

    有人能进一步解释一下deepcrawl的认证吗?我怎样才能使用卷曲器卷曲它 X-Auth-Token

    1 回复  |  直到 8 年前
        1
  •  2
  •   L. Amigo    8 年前

    卷曲有时可能很棘手。

    当我遇到CURL的问题时,我首先尝试在命令行上做同样的事情,在这种情况下,它工作得很好,因此我们可以拒绝API或CURL上存在任何类型的问题,因此我们必须假设问题在php调用上。

    当您得到空值时,很可能在curl处理程序上设置了错误,您只需查找它并将其显示在屏幕上:

        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_URL,"https://api.deepcrawl.com/accounts/00000/projects/195334/crawls/1306396/reports/thin_pages_basic");
    
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-Auth-Token:Private'));
    
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $server_output2 = curl_exec ($ch);
    
        if (curl_errno($ch)) {
            print curl_error($ch);
            die();
            // or whatever you might want to do with this error
           } 
        curl_close ($ch); 
    
        $results = json_decode($server_output2);
    

    在这种情况下,错误是:

    SSL证书问题:无法获取本地颁发者证书

    Here 您可以看到如何解决此问题,只需将有效的CA证书添加到php.ini(在使用cacert的示例中),如下所示:

    https://curl.haxx.se/ca/cacert.pem

    2) 将以下行添加到php.ini(如果这是共享主机,而您没有访问php.ini的权限,那么您可以将其添加到public_html中的.user.ini)

    curl.cainfo=“/path/to/download/cacert.pem”