CURL无法运行URL并返回302

时间:2014-05-20 05:27:21

标签: php rest curl http-status-code-302

我正在尝试通过CURL运行一个URL(具有注销功能)。但它返回302 http代码。当我通过POSTMAN(谷歌浏览器插件)或海报(Firefox插件)运行时,相同的网址,然后返回正确的结果({“状态”:“成功”})。任何帮助将不胜感激。

网址(JAVA应用网址):http://website.mywebsite.com:8083/VideoBook/signout.action

我的代码:

 // Open log file
        $logfh = fopen("GeoserverPHP.log", 'w') or die("can't open log file");

        // Initiate cURL session
        $service = "http://website.mywebsite.com:8083/VideoBook/";


$request = "signout.action";

             $url = $service . $request;
        $ch = curl_init($url);

        // Optional settings for debugging
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_VERBOSE, true);


 curl_setopt($ch, CURLOPT_STDERR, $logfh);

  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_COOKIESESSION, true);

curl_setopt($ch, CURLOPT_REFERER, true); 
curl_setopt($ch, CURLOPT_COOKIEJAR, true); 
curl_setopt($ch, CURLOPT_COOKIEFILE, true); 
    //Required GET request settings


  //  $passwordStr = "geosolutions:Geos";
       // curl_setopt($ch, CURLOPT_USERPWD, $passwordStr);

         //GET data
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));

        //GET return code
        $successCode = 200;

        $buffer = curl_exec($ch);

        echo "CURL INFO : <BR/> " ; 

        print_r(curl_getinfo($ch));

        echo "CURL OUTPUT  : <BR/> " ; 

        print_r($buffer);  
        // Check for errors and process results
        $info = curl_getinfo($ch);
        if ($info['http_code'] != $successCode) {
          $msgStr = "# Unsuccessful cURL request to ";
          $msgStr .= $url." [". $info['http_code']. "]\n";
          fwrite($logfh, $msgStr);
        } else {
          $msgStr = "# Successful cURL request to ".$url."\n";
          fwrite($logfh, $msgStr);
        }
        fwrite($logfh, $buffer."\n");

        curl_close($ch);
        fclose($logfh);

浏览器输出:

   CURL INFO : 
 Array
(
    [url] => http://website.mywebsite.com:8083/VideoBook/signout.action
    [content_type] => 
    [http_code] => 302
    [header_size] => 254
    [request_size] => 105
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.58976
    [namelookup_time] => 0.004162
    [connect_time] => 0.297276
    [pretransfer_time] => 0.297328
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 0.589739
    [redirect_time] => 0
    [redirect_url] => https://hpecp.mywebsite.com:8443/cas/login?service=http%3A%2F%2Fwebsite.mywebsite.com%3A8083%2FVideoBook%2Flogin.action
    [primary_ip] => 125.21.227.2
    [certinfo] => Array
        (
        )

    [primary_port] => 8083
    [local_ip] => 10.0.0.8
    [local_port] => 50710
)
CURL OUTPUT  : 

日志文件详细信息:

* Hostname was NOT found in DNS cache
*   Trying 125.21.227.2...
* Connected to website.mywebsite.com (125.21.227.2) port 8083 (#0)
> GET /VideoBook/signout.action HTTP/1.1
Host: website.mywebsite.com:8083
Accept: application/json

< HTTP/1.1 302 Moved Temporarily
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Location: https://hpecp.mywebsite.com:8443/cas/login?service=http%3A%2F%2Fwebsite.mywebsite.com%3A8083%2FVideoBook%2Flogin.action
< Content-Length: 0
< Date: Tue, 20 May 2014 06:02:29 GMT
< 
* Connection #0 to host website.mywebsite.com left intact
* Issue another request to this URL: 'https://hpecp.mywebsite.com:8443/cas/login?service=http%3A%2F%2Fwebsite.mywebsite.com%3A8083%2FVideoBook%2Flogin.action'
* Hostname was NOT found in DNS cache
*   Trying 15.126.214.121...
* Connected to hpecp.mywebsite.com (15.126.214.121) port 8443 (#1)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* Unknown SSL protocol error in connection to hpecp.mywebsite.com:8443 
* Closing connection 1
# Unsuccessful cURL request to http://website.mywebsite.com:8083/VideoBook/signout.action [302]

4 个答案:

答案 0 :(得分:5)

尝试添加ssl verify false并关注位置,现在全部设置

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

//输出: -

CURL INFO:

Array ( [url] => https://exampl.com:8443/cas/login?service=http%3A%2F%2Fexample%3A8083%2FVideoBook%2Flogin.action [content_type] => text/html;charset=UTF-8 [http_code] => 200 [header_size] => 593 [request_size] => 273 [filetime] => -1 [ssl_verify_result] => 18 [redirect_count] => 1 [total_time] => 3.073 [namelookup_time] => 0 [connect_time] => 0.577 [pretransfer_time] => 1.794 [size_upload] => 0 [size_download] => 8003 [speed_download] => 2604 [speed_upload] => 0 [download_content_length] => 8003 [upload_content_length] => -1 [starttransfer_time] => 2.387 [redirect_time] => 0.686 )

您需要检查最终的身份验证凭据

答案 1 :(得分:3)

我认为,添加这三个参数CURLOPT_REFERER,CURLOPT_COOKIEJAR,CURLOPT_COOKIEFILE和一个有效的cookie文件可以解决这个问题。我没有测试过代码。

答案 2 :(得分:1)

为了退出任何类型的会话,您首先需要登录,因此该服务必须期待对现有会话的一些引用。

要么您希望提供有关应该注销哪个用户的信息,要么在一系列对其他服务的调用之后将脚本记录下来。

它不能做的是自动注销正在访问您页面的用户,因为它无法看到它们。该请求完全源自您的服务器,并且仅包含您使用CURL传递给它的信息。除非您的脚本位于同一域中,否则您也无法向其提供浏览器所具有的信息,因为浏览器不会将您的脚本传递给其他站点设置的cookie。

答案 3 :(得分:1)

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

做好这份工作。