Jump to content

How to store sessions after performing a cURL post request


Tom.
 Share

Recommended Posts

I was wondering if anyone else has come across this? I'm wanting to store information from a cUrl request in a session. The cUrl goes to an external API and does a POST request. I'm wanting to store the session ID after using curl, but I've found that setting any $session after cUrl will cause it to forget all $session set if you leave the page.

It's such an annoying, weird thing that PHP does. I've tried using httpWire but that doesn't store the $session but at least it doesn't forget all other session data. 

Interestingly, the session is only reset if I try to use any of the data from the cUrl and store it in a session. Even if I set the response as a variable first. 

Link to comment
Share on other sites

Can we look at your code? How exactly did you try it?

Are you using something like

curl_setopt ($ch, CURLOPT_COOKIEJAR, COOKIE_FILE); 
curl_setopt ($ch, CURLOPT_COOKIEFILE, COOKIE_FILE); 

?

And did you try to use PHP's native $_SESSION instead of PW's $session?

Link to comment
Share on other sites

6 minutes ago, dragan said:

Can we look at your code? How exactly did you try it?

Are you using something like

curl_setopt ($ch, CURLOPT_COOKIEJAR, COOKIE_FILE); 
curl_setopt ($ch, CURLOPT_COOKIEFILE, COOKIE_FILE); 

?

And did you try to use PHP's native $_SESSION instead of PW's $session?

I'm using PW's $session. 

function post_curl($url, $data) {
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	$response = curl_exec($ch);
	curl_close($ch);

	return $response;
}

$token = $pages->get("template=foxycart")->token;

  if($_POST['ThisAction'] == "CheckLogin") {
    $login = post_curl("https://teatumjones.foxycart.com/v/2.0.0/api_json.php", $_POST);

    if($login) {
      $json = json_decode($login);

      if($json->ok === true || $json->details == "Transaction not found") {

        // Set session to logged in.
        $session->logged_in = true;

        // Find user from FoxyCart and store as session.
        $user = post_curl("https://teatumjones.foxycart.com/api", array(
          'api_token' => $token,
          'api_action' => 'customer_get',
          'customer_email' => $input->post->customer_email)
        );
        $xml = simplexml_load_string($user);

        $session->customer_id = $xml->customer_id;
      } else {
        echo "User or Password incorrect.";
      }
    } else {
      echo "Unable to reach login server at this time.";
    }
  }

Interestingly, the $session doesn't get removed if I don't use $session->customer_id = $xml->customer_id; I can use in it's place $session->customer_id = "Hello"; and that will be remembered. As soon as any $sessions are set using the $xml data. It just completely forget's all sessions set on that file. 

 

Link to comment
Share on other sites

Hey @Tom. - I am not sure your exact goal here - not sure if the session storage is for production use, or just while developing/testing, but @bernhard and I just put this together: https://processwire.com/talk/topic/12208-tracy-debugger/?do=findComment&comment=176842 - which came about while he was working with foxycart and since you are as well, I just thought it might be worth mentioning ?

  • Like 2
Link to comment
Share on other sites

11 hours ago, adrian said:

Does this help? https://stackoverflow.com/questions/33758126/how-to-store-xml-obj-in-php-session-variable

It might be related to the need to serialize the xml before storing. Is $xml->customer_id definitely a string?

Thank you all for your responses. This does in fact help! Turns out it wasn't a curl issue but trying to store and XML object as a session. 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...