Jump to content

Curl help


KSquared

Recommended Posts

Hello All,

Anyone help me with CURL?
Here is the issue:

I need to send some values through the post method from an interim page.
The form is submitted, data written to csv (which is working fine), then I need to send the same data to an email collection page outside of my domain.

The following script returns a page that says "Object Moved to here", "here" being a link to the actual page that needs to load. http://www.serenocanyon.com/thankyou.htm

[code]$SubscribeDate = date("F j, Y");
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$emailAdd = $_POST['emailadr'];
$source = $_POST['source'];
$thx = $_POST['thx'];    //http://www.serenocanyon.com/thankyou.htm
$err = $_POST['err'];
$usub = $_POST['usub'];

$postArray = "First Name=".urlencode($fname)."&Last Name=".urlencode($lname)."&Email Address=".urlencode($emailAdd)."&Source=".urlencode($source)."&thx=".urlencode($thx)."&err=".urlencode($err)."&usub=".urlencode($usub)."&Subscribe Date=".urlencode($SubscribeDate);

$url = "http://cl.exct.net/subscribe.aspx?lid=1220593";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArray);
$results = curl_exec($ch);
echo $results;
curl_close($ch);[/code]

Why is this happening?

Any help would be MUCH appreciated.
Thanks,
Keith
Link to comment
Share on other sites

You're problem looks like you're URL has parameters in it.

[code]
<?php

$url = "http://cl.exct.net/subscribe.aspx";
$postArray = "lid=1220593&First Name=". urlencode($fname) ."&Last Name=". urlencode($lname) ."&Email Address=". urlencode($emailAdd) ."&Source=". urlencode($source) ."&thx=". urlencode($thx) ."&err=". urlencode($err). "&usub=". urlencode($usub) ."&Subscribe Date=". urlencode($SubscribeDate);

// Initialize CURL
$ch = curl_init($url);
// Set "Fail on Error" to True
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
// Set "Allow Redirects" to True
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Set "Timeout" to 3 seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
// Set "POST" as Message Method
curl_setopt($ch, CURLOPT_POST, 1);
// Set POST Information
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArray);

// Use Output Buffering (Return Transfer doesn't work properly?)
ob_start();
// Execute CURL
curl_exec($ch);
// Get Information about the transaction
$info = curl_getinfo($ch);
// Close CURL Session
curl_close($ch);
// Store Data from Output Buffer
$data = ob_get_contents();
// Clear Output Buffer
ob_end_clean();

//200 is meaning valid url as 404 meaning "not found on this server" etc..
if ($info['http_code']==200) {
echo "success";
} else {
echo "failed";
}

?>
[/code]
Link to comment
Share on other sites

Thanks xsist,

You have no idea how long I have been trying to figure this out... I owe u big!
I got a "success" return. So... to actually load the thankyou page I edited the following:

[code]if ($info['http_code']==200) {
header("Location:http://www.serenocanyon.com/thankyou.htm");
} else {
header("Location:http://www.serenocanyon.com/error.htm");
}[/code]

Even though, the email collection page should send back the "return url" (being the thankyou.htm page) and load it, it does not, which is why I edited the above.

Does that seem correct?



Link to comment
Share on other sites

If the page you're posting to sets a cookie and you don't use CURLs cookie jar parameters, RETURNTRANSFER will not work because the page will be expecting a cookie and it will not be sent back.  Please read the manual as regards examples using RETURNTRANSFER.  This is important for the FOLLOWLOCATION parameter.

Hope this helps,

Dest
Link to comment
Share on other sites

[quote author=KSquared link=topic=111908.msg453850#msg453850 date=1161185787]
Even though, the email collection page should send back the "return url" (being the thankyou.htm page) and load it, it does not, which is why I edited the above.

Does that seem correct?
[/quote]

All the results from the page will be in $data. You can parse this for a result.
Link to comment
Share on other sites

Actually, I tried to parse $data but maybe Im parsing wrong.
If I do a var_dump($data), or print_r($data)<-- this give blank white page, it simply renders the thankyou.htm page on the email_updates.php page (where the curl script lives), it does not redirect to the actual thankyou.htm page.

However, i did the following with $info and it seems to work great, its returning correctly, even the error pages are now rendering correct with the correct errorcodes:

[code]if($info['http_code']==200){
header("Location:".$info['url']);
}else{
header("Location:".$info['url']);
}[/code]

Just want to be sure its ok to parse the $info instead of $data?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.