Help with a problem trying to pass information between to sites

I have a website lets call it "mainsite.com" this site has a registration form. On the same server in a folder I have another site lets call it mainsite.com/testsite (testsite is being developed using codeIgniter, mainsite.com was developed using a custom framework)

on testsite i have a page that adds users to a list either using a form or a url like www.mainsite.com/testsite/chimp?email=test@gmail.com&fname=test&lname=ting

<?php
class Chimp extends CI_Controller{

    /**
     * @var dataStorage
     */
    private $data;
    /**
     * Handle incoming request for
     */
    public function index() {
        if ($this->input->is_ajax_request()){
            $result = $this->_subscribe();
            echo json_encode($result);
            exit;
        }else{
            if(isset($_GET['email']))
            {$usr_email = urldecode($_GET['email']);
            $usr_fname = $_GET["fname"];
            $usr_lname = $_GET["lname"];
            //echo $usr_email ;
            $result = $this->_subscribenow($usr_email,$usr_fname,$usr_lname);
            echo json_encode($result);}
            
        
            //$this->load->view('home/inc/public_header_view');
            //$this->load->view('home/chimp_view', $this->data);
            //$this->load->view('home/inc/public_footer_view');
        }
    }
    
    //************************************************************
    
    /**
    * Handle subscription
    * @return string
    */
    private function _subscribe(){
        $success = true;
        if(!$this->input->get('firstname')){
            $message =  "No firstname provided";
            $success = false;
        }
        if($success && !$this->input->get('lastname')){
            $message =  "No lastname provided";
            $success = false;
        }
        if($success && !$this->input->get('emailaddress')){
            $message =  "No email address provided";
            $success = false;
        }
        if ($success){
            $this->load->library('Mcapi', array(
                'apikey' => $this->config->item('mailchimp_key')
            ));
            if ($this->mcapi->listSubscribe($this->config->item('mailchimp_list_id'), $this->input->get('emailaddress'), array(
                'FNAME' => $this->input->get('firstname'),
                'LNAME' => $this->input->get('lastname'),
            )) === true){
                //  It worked!
                $message = 'Success! Check your email to confirm sign up.';
            } else {
                $success = false;
                //  An error ocurred, return error message
                $message =  'Error: ' . $this->mcapi->errorMessage;
            }
        }
        return array('message' => $message, 'success' => $success);
    }
    private function _subscribenow($em,$fn,$ln){
        $success = true;
        echo "hi trevor ".$em;
        echo " ".$fn;
        echo " ".$ln;
            
        if ($success){
            $this->load->library('Mcapi', array(
                'apikey' => $this->config->item('mailchimp_key')
            ));
            if ($this->mcapi->listSubscribe($this->config->item('mailchimp_list_id'), $em, array(
                'FNAME' => $fn,
                'LNAME' => $ln
            )) === true){
                //  It worked!
                $message = 'Success! Check your email to confirm sign up.';
            } else {
                $success = false;
                //  An error ocurred, return error message
                $message =  'Error: ' . $this->mcapi->errorMessage;
            }
        }
        return array('message' => $message, 'success' => $success);
    }
}

Open in new window

I want the registration details entered on the mainsite.com form also sent to the page above so they are added to that list as well.

I am learning, I have tried using CURL from the submit form but i have something wrong. I hope this makes sense to someone and I am still searching for the best solution. thanks
//Trev Start
        $url = 'http://www.mainsite.com/testsite'; //NO TRAILING SLASH
        //print_r($user[0]['id']);
        //print_r($user);
        $sdata = array(
            'email' => $var['email'],
            'fname' => $var['fname'],
            'lname' => $var['lname']
            );
        $fields = "";
        foreach ($sdata as $key => $value){ 
            $fields .= "$key=" . urlencode($value) . "&";
        }
        $ch = curl_init($url . '/chimp');
        curl_setopt($ch, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, '50');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); // Returns response data instead of TRUE(1)
        curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim($fields, "& ")); // use HTTP POST to send form data
                
        //echo rtrim($fields, "& ");
        
        curl_exec($ch); //execute post and get results
        

        //temp $resp = curl_exec($ch); //execute post and get results
        curl_close($ch);
        //temp $result = json_decode($resp);
        
        ////print_r($result);
        //return $result;
    //Trev End

Open in new window

TrevorDavidAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
I don't know about the rest of it but you are trying to use two different methods to access 'chimp'.  The first with this URL "www.mainsite.com/testsite/chimp?email=test@gmail.com&fname=test&lname=ting" is a GET method but your curl routine is declaring a POST routine.  If you really want to make it work with both methods, you have to adjust the code to use $_REQUEST variables to receive the data instead of $_GET or $_POST.  $_REQUEST includes both.  http://php.net/manual/en/reserved.variables.request.php
1
Ray PaseurCommented:
The professional way to do something like this is to use an API.  This article gives the background about APIs.  One of the sites becomes the client and the other becomes the server.  This article explains the client/server relationship.

Requests come from the client; responses are created by the server.  GET method requests are information-only and must not modify the data model on the server.  So if your client is sending data that is to be kept on the server, you want to use a POST request (or PUT, but that is less commonly used).  

When you visit a web page, your initial view of the web page is acquired with a GET request.  The problem with using GET to modify the data model becomes obvious when you think about the behavior of search engines.  They will pre-fetch every URL on a web page, thus triggering all of the GET requests.  If any of these requests modified the server, the mere act of looking at a web page could damage the data model.  For this reason it's important that your application uses POST requests when you want to make a permanent change to the data model.

Most PHP functions return a value.   Let's consider this function call from line 23 of the second code snippet.
curl_exec($ch); //execute post and get results

Open in new window

The comment is correct; that is what you want to do, but the code is wrong because it never gets the result.  Instead, you might want that code to look more like this:
$response = curl_exec($ch); //execute post and get results

Open in new window

Now you can begin the debugging process because you have a value in $response that contains the cURL result.  If cURL returns FALSE, you can examine the cURL error information, etc.

This is a sample script that will make a cURL POST request and return a response object.  The object will contain the result document, as well as error information, if any, from cURL.
<?php // demo/temp_trevordavid.php

/**
 * http://www.experts-exchange.com/questions/28711706/Help-with-a-problem-trying-to-pass-information-between-to-sites.html
 *
 * Demonstrate how to use cURL to make a POST request
 * This may be used to start an asynchronous process
 *
 * http://php.net/manual/en/book.curl.php
 * http://curl.haxx.se/libcurl/c/libcurl-errors.html
 */
error_reporting(E_ALL);

Class Post_Response_Object
{
    public $href, $title, $http_code, $errno, $info, $document;

    public function __construct($href, $post_array=[], $title=NULL)
    {
        // AVOID TIMEOUT FOR LONG RUNNING SCRIPT
        set_time_limit(10);

        // STORE THE CALL INFORMATION
        $this->href  = $href;
        $this->title = $title;

        // PREPARE THE POST STRING
        $post_string = NULL;
        foreach ($post_array as $key => $val)
        {
            $post_string .= $key . '=' . urlencode($val) . '&';
        }
        $post_string = rtrim($post_string, '&');

        // MAKE THE REQUEST
        if (!$this->my_curl($href, $post_string))
        {
            // ACTIVATE THIS TO SEE THE ERRORS AS THEY OCCUR
            // trigger_error("Errno: $this->errno; HTTP: $this->http_code; URL: $this->href", E_USER_WARNING);
        }
    }

    protected function my_curl($url, $post_string, $timeout=3)
    {
        // PREPARE THE CURL CALL
        $curl = curl_init();
        curl_setopt( $curl, CURLOPT_URL,            $url           );
        curl_setopt( $curl, CURLOPT_HEADER,         FALSE          );
        curl_setopt( $curl, CURLOPT_POST,           TRUE           );
        curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string   );
        curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate' );
        curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout       );
        curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE           );
        curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE           );
        curl_setopt( $curl, CURLOPT_FAILONERROR,    TRUE           );

        // IF USING SSL, THIS INFORMATION MAY BE IMPORTANT
        // http://php.net/manual/en/function.curl-setopt.php#110457
        // http://php.net/manual/en/function.curl-setopt.php#115993
        // http://php.net/manual/en/function.curl-setopt.php#113754
        // REDACTED IN 2015 curl_setopt( $curl, CURLOPT_SSLVERSION, 3 );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, FALSE  );
        curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE  );


        // RUN THE CURL REQUEST AND GET THE RESULTS
        $this->document  = curl_exec($curl);
        $this->errno     = curl_errno($curl);
        $this->info      = curl_getinfo($curl);
        $this->http_code = $this->info['http_code'];
        curl_close($curl);

        // RETURN DOCUMENT SUCCESS SIGNAL
        return $this->document;
    }
}


// USAGE EXAMPLE CREATES ASSOCIATIVE ARRAY OF KEY=>VALUE PAIRS
$args["name"] = 'Ray';
$args["mail"] = 'Ray.Paseur@Gmail.com';

// SET THE URL
$url = "http://YOUR_API_ENTRY_POINT_HERE";

// CREATE THE RESPONSE OBJECT
$resp = new POST_Response_Object($url, $args);

// SHOW THE RESPONSE OBJECT
echo '<pre>';
var_dump($resp);

Open in new window

HTH, ~Ray
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.