Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

cURL not working!

Posted on 2009-12-20
9
Medium Priority
?
495 Views
Last Modified: 2012-05-08
This was originally posted by DrDamnit and he fixed his own problem.

I'm curious of what the solution is.

============================================

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

0
Comment
Question by:JeffreyLevesque
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 32

Expert Comment

by:DrDamnit
ID: 26091594
I meant... post your specific question, so that I can help you with your question. :-)
0
 
LVL 32

Expert Comment

by:DrDamnit
ID: 26091599
Mostly, because I don't remember what the solution was. Just that I fixed it. :-)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26091698
If it is here at EE, please post a link to the original question.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:JeffreyLevesque
ID: 26091710
Hi Ray,

What I posted above is word for word except for my starting comment.

Here's the link:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_24632374.html
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26091724
Yes, I remember that now from back in August - that is one reason why EE encourages us to post the solutions, even when we solve it ourselves!
0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 26092246
try:
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);
        }
 
        function add_var($field,$value) {
                array_push($this->fields,$field);
                array_push($this->values,$value);
		return this;
        }
		function encode_params($method='GET'){
			$str = "";
			//Compile POST string
               for($i=0;$i<=sizeof($this->fields);$i++) {
				$str.= "&" .$this->fields[$i] . "=" . urlencode($this->values[$i]);
               }
			$str=substr($str,1);
			if('GET'==strtoupper($method))
			{
				$str="?".$str;
			}
			return $str;
		}
        function post_this() {
                
 
 
                curl_setopt($this->ch, CURLOPT_RETURNTRANSFER  ,1);
                curl_setopt($this->ch, CURLOPT_POST,1);
                curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->encode_params('POST') );
                $this->response = curl_exec($this->ch);
                if($this->debug) {
                        echo "<pre>Sent:\n{$buffer}";
                        echo "\nReceived:\n{$this->response}</pre>";
                }
		return $this;
        }
	   function get_result(){
	   	return $this->response;
	   }
 }
 
$obj = new awebber();
echo $obj->add_var("username","john")
		->add_var("password","secret")
		->post_this()
		->get_result();

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 26092272
Ignore lines 1-15 on my previous post.
0
 

Author Comment

by:JeffreyLevesque
ID: 26092339
No worries on lines 1-5

I've been with Aweber since Tom opened shop and am very familiar with
the forms themselves.

I'll check out your solution for this one in a bit.

I'm so elated with the XML solution you just gave me that I'm going for a ride on
the Harley real quick to celebrate!

Hmmm, I wonder if this can be combined with the XML solution to kill two birds with one stone!

If anyone reading this needs a solution for manipulating an XML response from a service that
you have no control over, go check out hielo's solution here:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_24992444.html

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month20 days, 21 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question