php - CURL 无法运行 URL 并返回 302

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

我正在尝试通过 CURL 运行一个 URL(具有注销功能)。但它返回 302 http 代码。当我运行 POSTMAN(Google Chrome 插件)或 POSTER(Firefox 插件)时使用相同的 url,然后返回正确的结果({"status":"success"})。任何帮助将不胜感激。

URL(JAVA 应用程序 URL):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]

最佳答案

尝试添加 ssl verify false 和 follow location,现在一切就绪

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

//输出:-

curl 信息:

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 )

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

关于php - CURL 无法运行 URL 并返回 302,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23751634/

相关文章:

javascript - 如何使这个 CSS 联系表单与 javascript 一起工作?

javascript - 在 Marklogic 中,我有一个自定义 JavaScript 函数。如何通过 REST API 调用它?从CURL调用它的过程是怎样的?

C++ CURL Json 请求不一致 API 以进行消息发布

php - 如何获取javascript转换后的html源代码?

php - 从 MySQL Google Maps v3 上的表中删除标记

php - 如何将 2 个 SQL 查询放在一起?

php - JSON 和 PHP 问题

java - 使用 REST API 创建新站点

Django 休息框架 : Disable field update after object is created

java - 如何用 Java 发布 https 帖子?