【问题标题】:Make the called url deal with client cookie not caller cookie使被调用的 url 处理客户端 cookie 而不是调用方 cookie
【发布时间】:2013-03-04 11:56:22
【问题描述】:

我正在制作一个处理API的应用

所以我尝试使用cURL 调用API,问题是api 将处理执行此curl 调用的服务器cookie,而不是使用应用程序的客户端cookie...

例如:客户端已经登录api, 但是当我检查客户端是否通过 curl 调用登录时,您未授权的 api 响应

那么我怎样才能让API 处理客户端cookie 或将客户端的cookie 传递给API

【问题讨论】:

    标签: php session cookies curl session-cookies


    【解决方案1】:

    您需要使用 cookiefile,如下所示:

            # Set the cookiefile name, which will allow us to store the cookie and present it later for all requests that require it...
            $cookiefile = tempnam("/tmp", "cookies");
            $agent     = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
    
            # Set the user name and password values
            $user      = $argv[1];
            $password      = $argv[2];
    
            # The API url, to do the login
            $url = "https://some_site.com/login.php?WID=$user&PW=$password";
    
            # Initialise CURL
            $ch = curl_init();
    
            # Set all the various options
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_userAGENT, $agent);
            curl_setopt($ch, CURLOPT_POST, 0); // set POST method
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
            curl_setopt($ch, CURLOPT_userPWD, $user.":".$password);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    
            # Execute the first CURL request to perform the login...
            $results = curl_exec($ch);
    
            # Setup our next request...
            $job_id_number      = $argv[3];
            $url = "https://some_site.com/request.php?task=add&taskID=$job_id_number";
    
            # We do not have to initialise CURL again, however we do need to adjust our URL option for the second request...
            curl_setopt($ch, CURLOPT_URL, $url);
    
            #Remember to use the same cookiefile as above  
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    
            # Execute the second CURL call  to perform the action (using the cookie we retrieved from earlier)
            $results = curl_exec($ch);
    
            echo "$results";
    

    【讨论】:

      【解决方案2】:

      您应该查看 API 的文档。 也许文档描述了一种授权客户端的方法。

      这样它就行不通了,因为 cookie 是在客户端(在浏览器中)设置的,而不是在您的服务器上。因此,当您进行 cURl 调用时,cookie 数据不会包含在请求中。 通过 Javascript 调用 API 可以工作,因为这是客户端发送的请求(带有所需的 cookie)

      【讨论】:

      • 我知道 javascript 调用有效,但我需要使用 PHP
      • 然后您必须读出所有 cookie 并将它们与您的 cURL 请求一起发送。这只有在 cookie 是从您的域中设置时才有可能。
      猜你喜欢
      • 2019-01-31
      • 2011-05-06
      • 2017-05-12
      • 2014-01-24
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      相关资源
      最近更新 更多