Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 876689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:28:24+00:00 2026-05-15T11:28:24+00:00

I am making a class to comunicate with our company API… I’m using curl

  • 0

I am making a class to comunicate with our company API…
I’m using curl to post data and retrieve the response in json.
In the last part of the code (after the class), i log the user into the server (it gives true), but when i try to connect again with curl, he says that i’m not logged!
I’ve done a google search already and added the curl_setopt($ch,CURLOPT_COOKIE,$this->Session_Cookie); line.

Can anybody give me a light on this?

Thanks in advance 😉

if(!class_exists("Microdual")) {
 class Microdual{

  // No caso de a sessão não estar iniciada, iniciar aqui a sessão


  ################
  ################
  ################
  ################ Iniciar funcoes privadas ################

  private function Extra_LoadSession($varname,$otherwise){
   return (!empty($_SESSION[$this->Session_Prefix . $varname])) ? $_SESSION[$this->Session_Prefix . $varname] : $otherwise;
  }
  private function Extra_SaveSession($varname,$value){
   $_SESSION[$this->Session_Prefix . $varname] = $value;
   return true;
  }

  /**
  * $this->API_Comunicate() "Comunicar comandos com os servidores Microdual (enviar e receber)"
  *
  * @param data array "Colocar as variaveis que deseja passar à plataforma (Ver Lista completa de variaveis no Inicio)"
  *
  * @return array or void (false)
  */
  private function API_Comunicate($_data){

   // Converter o array em string (serialize)
   $data = array();    
   while(list($n,$v) = each($_data)){
    $data[] = "$n=$v";
   }
   $data = implode('&', $data);
   // format --> test1=a&test2=b etc.

   $ch = curl_init();
   curl_setopt($ch,CURLOPT_URL,$this->Geral_URLAPI);
   curl_setopt($ch,CURLOPT_POST,count($_data));
   curl_setopt($ch,CURLOPT_COOKIE,$this->Session_Cookie);
   curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
   curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
   $content = curl_exec($ch);
   curl_close($ch);

   if(($content !== false) && (!empty($content))){
    return json_decode($content, true);
   }else{
    return false;
   }
  }


  ################
  ################
  ################
  ################ Iniciar funcoes Públicas ################

  public function Debug_VarDump($varname){
   $string = "<pre>";
   $string .= var_dump($varname);
   $string .= "</pre>";
   return $string;
   exit;
  }
  /**
  * $this->IsLogged() "Verificar se está autenticado no servidor (primeiro localmente, e depois liga ao servidor)"
  *
  * @return void
  */
  public function IsLogged(){
   if($logged) return true;
   $logged = $this->Extra_LoadSession("Login_Logged",false);
   if($logged){
    return true;
   }else{
    // Conectar ao servidor
    $dados = $this->API_Comunicate(array());
    if($dados!==false){
     if(!empty($dados['auth']['logged'])){
      return $dados['auth']['logged'];
     }else{
      return false;
     }
    }else{
     return false;
    }
   }
  }

  /**
  * $this->Login() "Executar o Login nos servidores Microdual"
  *
  * @param username string "Colocar aqui o nome de utilizador da sua conta em www.microdual.com"
  * @param password string "Colocar aqui a password da sua conta em www.microdual.com"
  *
  * @return void
  */
  public function Login($username,$password){
   if(empty($username) || empty($password)) return false;
   if($this->IsLogged()) return true;

   $receive = $this->API_Comunicate(array(
    "type" => "auth",
    "action" => "add",
    "auth_username" => $username,
    "auth_password" => $password
   ));

   if($receive["auth"]["status"] && $receive["auth"]["logged"]){
    $this->Extra_SaveSession("Login_Logged",true);
    $this->Login_Logged = true;
    return true;
   }else{
    return false;
   }
  }
  /**
  * $this->SMS_Send() "Executar o Login nos servidores Microdual"
  *
  * @param number string "Colocar aqui o numero do telemovel para enviar sms"
  *
  * @return void
  */
  public function SMS_Send($number,$msg){
   // Guardar apenas os numeros
   $number = preg_replace("/[^0-9]/", "", $number);
   $msg = trim($msg);

   $receive = $this->API_Comunicate(array(
    "type" => "sms",
    "action" => "add",
    "sms_to" => $number,
    "sms_msg" => $msg
   ));
   return $receive;
  }

  ################
  ################
  ################
  ################ Iniciar variaveis da class ################

  private $Session_Prefix;
  private $Session_Cookie;
  private $Geral_URLAPI;
  private $Login_Logged;




  ################
  ################
  ################
  ################ Iniciar dados da class ################

  function __construct(){
   $this->Session_Prefix = "MYCMSAPI_";
   $this->Session_Cookie = "PHPSESSID=".$_COOKIE['PHPSESSID']."; path=/";
   $this->Geral_URLAPI = "http://www.MYCOMPANY.com/MyapiURL";
   $this->Login_Logged = $this->Extra_LoadSession("Login_Logged",false);
  }
 }
}

$Microdual = new Microdual();
if($Microdual->Login("usernamehere","password")){
 $Microdual->Debug_VarDump($Microdual->SMS_Send("93211254","Teste Test Hi :)"));
}else{
 echo "Login com erro";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:28:25+00:00Added an answer on May 15, 2026 at 11:28 am

    The CURLOPT_COOKIE option is for sending a particular cookie using CURL. The options you are looking for are CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR, which specify a file to save and load cookie from.

    So you’ll have to do something like this:

    curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a class Customer that has the following data members and properties:
I'm making a class which needs to provide me with all the data from
I'm making a Class Library and I'm using it inside a Windows Form Program.
Here is my problem. I have created a pretty heavy readonly class making many
I was making a simple class for a small project and decided to just
I tried making a wrapper class that encapsulates an object, a string (for naming
I ran into the situation where my class is making one asynchronous web call
I am working on a project with MySQL,Hibernate,Java,GWT I am making an entity class
I am having a little design crisis, I am making a Plane class, which
Im making a login/logout class that logs users in, sets cookies based on user's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.