CURL cannot run URL and return 302
Asked Answered
A

4

6

I'm trying to run an URL (which have signout functionality) through the CURL. But it is returning 302 http code. Same url when i run through the POSTMAN ( Google Chrome addon ) or POSTER ( Firefox Addon) , then it is return proper result ( {"status" : "success" } ). Any help would be greatly appreciated.

URL (JAVA APPLICATION URL) : http://website.mywebsite.com:8083/VideoBook/signout.action

MY CODE :

 // 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);

OUTPUT IN BROWSER :

   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  : 

LOG File Details :

* 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]
Ambulate answered 20/5, 2014 at 5:27 Comment(6)
can we check with actual url? may be you need to show original requestBuonomo
Rakesh Sharma, i have updated my questionAmbulate
302 is redirection status. what response you got in postmanA1
I have updated my code. Still it's not workingAmbulate
then you need to check your values and required format to get result CURL is working nowBuonomo
Thanks for all your response. I have used some feature in the java application to run this signout url. Now My issues is solved.Ambulate
B
11

try to add ssl verify false and follow location and now all set

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

//output:-

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 )

You so need to check auth credentials on your end

Buonomo answered 20/5, 2014 at 5:43 Comment(1)
i got this result so curl is ok you need to check other processBuonomo
L
3

I think, adding these three parameter CURLOPT_REFERER, CURLOPT_COOKIEJAR, CURLOPT_COOKIEFILE and an valid cookie file can solve this. I didn't tested the code.

Lading answered 20/5, 2014 at 5:50 Comment(11)
I think this answer make sense to me. may be worth check with CURLOPT_COOKIESESSIONA1
i have updated my question with your code . curl_setopt($ch, CURLOPT_COOKIESESSION, true); curl_setopt($ch, CURLOPT_REFERER, true);. curl_setopt($ch, CURLOPT_COOKIEJAR, true); curl_setopt($ch, CURLOPT_COOKIEFILE, true);. Its not workingAmbulate
your config is not correct COOKIEJAR and COOKIEFILE should have the path to valid file (writable)A1
You should use cookie in this way $tmpfname = dirname(FILE).'/cookie.txt'; curl_setopt($session, CURLOPT_COOKIEJAR, $tmpfname); curl_setopt($session, CURLOPT_COOKIEFILE, $tmpfname); and in cookie.txt you should set valid cookieLading
Note that that should read dirname(__FILE__), but the formatter turned it into bold text.Charwoman
ohh yes that was a mistake. sorry for that. and thanks for defining it @CharwomanLading
ok. Actually, We are not maintaining cookie in that url. We are only maintaining session. So is there any way to send the session through that URL ? or else i have to maintain the COOKIE?Ambulate
this will work for session also , in that case in cookie.txt file you have to store sessionLading
Actually, i tried to access the cookie of website.vmokshagroup.com from the hpecp.vmokshagroup.com in our CURL , But i can't able to access the cookie or session data. Because both are different website. So i can't with the cookie data to the cookies.txt file and map it to the CURLOPT_COOKIEJARAmbulate
in my case the value of session of website.vmokshagroup.com is .105528584.log.optimizely.com TRUE / FALSE 1715763477 end_user_id oeu1400403476741r0.9784319167908209 you should put your session value to cookie.txtLading
HTTP has no notion of sessions. A session is something the application maintains, usually using a cookie to mark it. However, as I mentioned in my answer, your PHP script cannot see the cookies that your browser holds for an unrelated site. You can't write a script that will log me out of Facebook, for instance, unless you are able to host it somewhere on facebook.com.Charwoman
C
2
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Do the job.

Crossed answered 27/3, 2018 at 21:37 Comment(0)
C
1

In order to log out of any kind of session, you first need to be logged in, so the service must be expecting some reference to an existing session.

Either it expects you to give it information about which user should be logged out, or it is intended to log your script out after a series of calls to other services.

What it cannot do is automatically log out the user who is accessing your page, because it has no way of seeing them. The request originates entirely on your server, and only contains the information you pass to it with CURL. Nor will you be able to give it the information a browser would have, unless your script is on the same domain, as the browser will not pass your script the cookies set by the other site.

Charwoman answered 20/5, 2014 at 6:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.