is curl_multi for me?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DirtyBird
Forum Newbie
Posts: 8
Joined: Wed May 26, 2010 6:40 pm

is curl_multi for me?

Post by DirtyBird »

I have a form (form A) that is being submitted cross domain to our CRM via cURL. Because of some "issues" we have a new ESP. I now need to submit two form fields from "form A" to a different domain. Can this be accomplished using the curl_multi functions? My form, and the data to be sent to the ESP both need to use cURL as they are on different domains. I have two questions.

1. How to combine the two separate cURL functions into one using cURL multi. I have read the curl_multi documentation and don't completely get it.
2. Would it be "bad practice" to simply have a hidden form (form B) that echos the necessary values of (form A) that submits to the ESP via its own cURL script?

I appreciate any guidance here.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: is curl_multi for me?

Post by John Cartwright »

Did you read the documentation @ http://ca.php.net/manual/en/function.cu ... i-exec.php ?

It's quite straight forward and multiple examples are given. Post your code and we can identify where you are having problems.
DirtyBird
Forum Newbie
Posts: 8
Joined: Wed May 26, 2010 6:40 pm

Re: is curl_multi for me?

Post by DirtyBird »

Thx John, I have read the code, but just getting the original cURL to work was a major milestone for me! I am a primarily a designer but have been pushed into a web developer role as well, not that that is an excuse for ignorance. Here is my original curl code

Code: Select all

<?php

$url = 'remote-server-with-processor-script';
$referred_by_text = $_POST["referred_by_text"];
$broker_text = $_POST["broker_text"];
$email1 = $_POST["email1"];
$radiobutton = $_POST["radiobutton"];
$trade_futures = $_POST["trade_futures"];
$trade_fx = $_POST["trade_fx"];
$trade_equities = $_POST["trade_equities"];

$fields = array(
	'referred_by_text'=>urlencode($referred_by_text),
	'broker_text'=>urlencode($broker_text),
	'email1'=>urlencode($email1),
	'radiobutton'=>urlencode($radiobutton),
	'trade_futures'=>urlencode($trade_futures),
	'trade_fx'=>urlencode($trade_fx),
	'trade_equities'=>urlencode($trade_equities)
);

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'& ');

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($ch);
echo $output;
curl_close($ch);

?>
And here is the curl script to send the "email1" value to the ESP per the ESP's API guide

Code: Select all

<?php
if ($_POST)
{
$smi_domain					= "http://xxx.com/";
$smi_url					= $smi_domain."api/";
$smi_action					= "Subscriber_Add";
$smi_api_key				= "XXX";
$smi_list_id				= "XXX";
$smi_values	= array
(
	"smi_api_key"					=> $smi_api_key,
	"smi_action"					=> $smi_action,
	"smi_list_id"					=> $smi_list_id,
	"smi_email"						=> $_POST['email_address'],
	"smi_name"						=> $_POST['name'],
	"smi_test_request"				=> "FALSE"
);
$fields = "";
foreach ($smi_values as $key => $value)
	$fields .= "$key=" . urlencode ($value) . "&";
$ch = curl_init ($smi_url); 
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, rtrim ($fields, "& ")); 
$resp = curl_exec ($ch); 
curl_close ($ch);
$response = explode ("\n", $resp); 
if ($response[0]==1)
{
	echo "Success";
	echo "<br />";
	echo $response[2];
}
else
{
	echo "The following error was returned: ";
	echo ($response[1]);
}
}
?>
I can't figure out where to start with curl_multi to combine the two as their structure is different.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: is curl_multi for me?

Post by John Cartwright »

Using this example in the documentation and plugging in your curl request handlers:

Code: Select all

$ch1 = curl_init ($smi_url);
curl_setopt ($ch1, CURLOPT_HEADER, 0);
curl_setopt ($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch1, CURLOPT_POSTFIELDS, rtrim ($fields, "& "));

$ch2 = curl_init($url);
curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);

//create the multiple cURL handle
$mh = curl_multi_init();

//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$active = null;
//execute the handles
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
Simple huh. Modify as neccesary.
DirtyBird
Forum Newbie
Posts: 8
Joined: Wed May 26, 2010 6:40 pm

Re: is curl_multi for me?

Post by DirtyBird »

OK, a little more intelligent question this time. This doesn't seem to work, is there a glaring error here?

Code: Select all

<?php
//ESP API info
$smi_domain					= "ESPDomain";

// Do not change this value.
$smi_url					= $smi_domain."api/";

// This value sets the method.
$smi_action					= "Subscriber_Add";

// Please change the values below with the ones from the web application.
$smi_api_key				= "XXX";
$smi_list_id				= "XXX";

//Change the smi_test_request to TRUE for Test Mode
$smi_values	= array
(
	"smi_api_key"					=> $smi_api_key,
	"smi_action"					=> $smi_action,
	"smi_list_id"					=> $smi_list_id,
	"smi_email"						=> $_POST['email_address'],
	"smi_name"						=> $_POST['name'],
	"smi_test_request"				=> "FALSE"
);

//My curl info
$url = 'MYRemoteServerProcessorScript';
$referred_by_text = $_POST["referred_by_text"];
$broker_text = $_POST["broker_text"];
$email1 = $_POST["email1"];
$radiobutton = $_POST["radiobutton"];
$trade_futures = $_POST["trade_futures"];
$trade_fx = $_POST["trade_fx"];
$trade_equities = $_POST["trade_equities"];

$fields = array(
	'referred_by_text'=>urlencode($referred_by_text),
	'broker_text'=>urlencode($broker_text),
	'email1'=>urlencode($email1),
	'radiobutton'=>urlencode($radiobutton),
	'trade_futures'=>urlencode($trade_futures),
	'trade_fx'=>urlencode($trade_fx),
	'trade_equities'=>urlencode($trade_equities)
);

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

$ch1 = curl_init ($smi_url);
curl_setopt ($ch1, CURLOPT_HEADER, 0);
curl_setopt ($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch1, CURLOPT_POSTFIELDS, rtrim ($fields, "& "));

$ch2 = curl_init($url);
curl_setopt($ch2, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_POST, 1);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);

//create the multiple cURL handle
$mh = curl_multi_init();

//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);

$active = null;
//execute the handles
do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: is curl_multi for me?

Post by John Cartwright »

Define doesn't work.
DirtyBird
Forum Newbie
Posts: 8
Joined: Wed May 26, 2010 6:40 pm

Re: is curl_multi for me?

Post by DirtyBird »

The data is being posted according to firebug, but only ends up in my CRM, not the ESP. And there is no response (My form processor - '$url' has responses for success and failed posts and it simply goes to a blank page

I am trying to break this down into manageable problems. Here they are:
1. in my form the email input is "email1" and the _POST function is

Code: Select all

$email1 = $_POST["email1"];
for the API it is

Code: Select all

"smi_email"	 => $_POST['email_address'],
So I need to make

Code: Select all

"smi_email" => $_POST['email1']
I can get the data to post to my CRM, not the ESP and it doesn't behave correctly once its posted. From our landing pages it should redirect to a page on our site. On our in site log in forms, it displays a message based on the response form the remote script via AJAX.
Post Reply