0

我试图弄清楚如何使用 REST 并且卡住了。

这是我正在查看的文档:

Request: https://www.domain.com/shipping/packages?id=5123

/members/login/auth
Authenticates a user and sets the szsess cookie that must be passed into any subsequent API method.
Parameters
email – User's email address
pswd – User's password

我将使用什么 PHP 代码对用户进行身份验证,然后将 cookie 存储在可以传递给 API 的变量中?

这是我到目前为止所拥有的:

<?php
$request =  'https://www.domain.com/shipping/packages?id=5123'; 
$session = curl_init($request); 

print_r($session);
?>

我对所有 CURL 和 Rest 的东西都迷失了。

4

1 回答 1

1

[咆哮] 如果他们使用 cookie 进行身份验证,他们就不是 ReSTful。[/咆哮]

为了处理这个 API,你需要学习做以下事情(感谢他们粗制滥造的实现):

  1. 使用卷曲
  2. 使用 CURL 饼干罐

这两点很容易。就是有点蛋疼。为了让我们的生活更轻松,我们将在 PHP 中定义一个 API 包装器来执行调用。

<?php
class APIWrap {
    private $jarFile = false;
    function __construct($jarFile=null) {
       if ($jarFile) {
          if (file_exists($jarFile)) $this->jarFile = $jarFile;
          else {
              touch($this->jarFile);
              $this->jarFile = $jarFile;
          }
       }
       else {
           $this->jarFile = "/tmp/jar-".md5(time()*rand(1,10000000)).".cookie";
       }
    }
    /* The public methods */
    public function call_url($url) {
       return $this->_call_url($url);
    }
    public function logIn($email,$password) {
        return $this->_call_curl("https://www.domain.com/members/login/auth",array("email" => $email, "pswd" => $password));
    }
    /* Our curl channel generator */
    protected function _call_curl($url,$post=array()) {
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $url);
       if (count($post)) {
           curl_setopt($ch, CURLOPT_POST, true);
           curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
       }
       curl_setopt($ch, CURLOPT_COOKIEJAR, $this->jarFile);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
       return curl_exec($ch);
    }
}

随意设置 curlopt 调用 - 它们用于参考目的。重要的是CURLOPT_COOKIEJAR

于 2013-05-16T20:45:57.473 回答