Jump to content

Posting data to another URL with CURL


clay1

Recommended Posts

I am pretty unfamiliar with curl and could use some help.

 

I need to post the results of a db query to another site using curl.

 

So far for testing purposes using hardcoded values I have a simple single record version working

 

$url = "http://URLremove";
$fields = array (
"Emailaddress" => "[email protected]",
"first_name" => "first",
"last_name" => "last",
"number" => "234234",
"street" => "address",
"city" => "city",
"state" => "AX",
"zip" => "12345",
"gender" => "M",
"age" => 30,
"dialer_source_id" => 0,
"income" => 35000,
"profile_href" => "http://removed"
);


//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

 

My question is how do I do this so all my results get posted and the result(the 3rd party page will respond whether a record was accepted or not) is logged for each one?

Link to comment
https://forums.phpfreaks.com/topic/194694-posting-data-to-another-url-with-curl/
Share on other sites

Well, when you get the curl result the response should be stored in that variable. 

 

var_dump the response and see if you are getting that.  If so, you can just search the string for whatever gets posted if it's a success and move from there!

 

First step tho would be to dump the results, nad see what it's sayin!

This is getting annoying.

 

if($rows>0){
for($i= 0; $i<$rows; $i++){
$lead = pg_fetch_assoc($result);

$id = $lead['serial'];
list($firstname, $lastname) = explode(' ' , $lead['name']);
	switch ($lead['income']) {
    case 1:
        $incomerange = '25 - 35k';
        break;
    case 2:
        $incomerange = '36 - 50k';
        break;
    case 3:
        $incomerange = '51k - 100k';
        break;
	case 4:
        $incomerange = '>100k';
        break;
}


$fields = array (
"Emailaddress" => $lead['e-mail'],
"first_name" => $firstname,
"last_name" => $lastname,
"number" => $lead['telephone'],
"number2" => $lead['telephone2'],
"street" => $lead['street address'],
"city" => $lead['city'],
"state" => $lead['state/province'],
"zip" => $lead['zip/postal code'],
"gender" => $lead['gender'],
"age" => $lead['age'],
"dialer_source_id" => 0,
"income" => $incomerange,
"profile_href" => "http://removed$id"
);


//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
echo "$i <BR><br>";
var_dump($fields);

//open connection

$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);


}
}

 

My DB query is working. If I comment out the curl stuff and do a var_dump($fields) everything is correct. However when I run this with the curl part included the first record posts fine. However the second record I get:

 

Warning: pg_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/inthelea/domains/intheleadnightlife.com/public_html/elove/leadpost.php on line 25

 

And then the results from the 3rd party site are all wrong.. as if the script is trying to run pg_fetch_assoc on the 3rd party site not my own

OK. I fixed 1 issue

 

However I now have a more vexing problem.

 

When I run the script the first result comes back fine and is accepted:

 

Form Fields - struct

EMAILADDRESS 	[email protected] 

 

But then on the next iteration the results start stacking on top of each other and being rejected

 

EMAILADDRESS  	 [email protected],[email protected] 

 

This is what the URL is giving back to me. The fields I am posting are correct(I've done a var dump on them)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.