Link to home
Start Free TrialLog in
Avatar of DrDamnit
DrDamnitFlag for United States of America

asked on

cURL not working!

I am attempting to use cURL to integrate a form into awebber.com, which usually uses it's own dynamically generated forms to signup people to an opt-in list.

I am having a case of user error. I have created a class to POST the form variables to their url, but it doesn't appear to be working.

Code snippet 1: This form works, but is a straight HTML POSTed form.
Code snippet 2: This is the class I wrote, but it doesn't POST the variables correctly.

What is wrong with the class?
SNIPPET 1:
 
<form method="post" action="http://www.aweber.com/scripts/addlead.pl">
<input type="hidden" name="meta_web_form_id" value="1468401561">
    <input type="hidden" name="meta_split_id" value="">
    <input type="hidden" name="unit" value="jobhuntsuccess">
    <input type="hidden" name="redirect" value="http://www.aweber.com/form/thankyou_vo.html" id="redirect_89d25e6c2fded4b568b61d9895650a60">
    <input type="hidden" name="meta_redirect_onlist" value="">
    <input type="hidden" name="meta_adtracking" value="">
    <input type="hidden" name="meta_message" value="1">
    <input type="hidden" name="meta_required" value="from">
    <input type="hidden" name="meta_forward_vars" value="1"> 
<table><tr><td colspan=2><center><div>Uses http://www.guaranteedjobsystems.com/quiz/</div></center></td></tr><tr><td>Name:</td><td><input type="text" name="name" value="" size="20"></td></tr><tr><td>Email:</td><td><input type="text" name="from" value="" size="20"></td></tr>    <tr><td colspan=2><center></center></td></tr><tr><td align="center" colspan="2"><input type="submit" name="submit" value="Submit"></td></tr></table></form>
 
SNIPPET 2:
<?php
        //Uses cURL to send variables to aWebber.com to signup for a list.
class awebber {
 
        var $fields;
        var $values;
        var $ch;
        var $response;
        var $debug=false;
 
        function __construct($url = 'http://www.aweber.com/scripts/addlead.pl') {
                $this->fields = array();
                $this->values = array();
                $this->ch = curl_init($url);
//                curl_setopt($this->ch, CURLOPT_POSTFIELDS    ,$this->vars); //<-- Should be done in post_this instead.
//                curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION  ,1);
//                curl_setopt($this->ch, CURLOPT_HEADER      ,0);  // DO NOT RETURN HTTP HEADERS
        }
 
        function add_var($field,$value) {
                //adds POST vars to the vars array
                //usage $x->add_var(field,value)
                array_push($this->fields,$field);
                array_push($this->values,$value);
        }
 
        function post_this() {
                //create array to hold variables
                $buffer = array();
 
                //Compile POST string
                for($i=0;$i<=sizeof($this->fields);$i++) {
                        $buffer[$i][0] = $this->fields[$i];
                        $buffer[$i][1] = $this->values[$i];
                }
 
 
                //executes the cURL post function.
//                curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: multipart/form-data"));
                curl_setopt($this->ch, CURLOPT_RETURNTRANSFER  ,1);  // RETURN THE CONTENTS OF THE CALL
                curl_setopt($this->ch,CURLOPT_POST,1);
                curl_setopt($this->ch, CURLOPT_POSTFIELDS    ,$this->build_url($fields,$values));
                $this->response = curl_exec($this->ch);
                if($this->debug) {
                        echo "<pre>";
                        print_r($this->build_url($fields,$values));
                        echo "</pre>";
                        echo $this->response;
                }
        }
 
        function build_url($fields,$values) {
                for($i=0;$i<=sizeof($fields);$i++) {
                        $buffer .= $fields[$i] . "=" . $values[$i];
                        if($i < sizeof($fields))
                        {
                                $buffer .= "&";
                        }
                }
                return urlencode($buffer);
        }
}

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

What is the URL of the page you are trying to simulate?
ASKER CERTIFIED SOLUTION
Avatar of DrDamnit
DrDamnit
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial