Jump to content


Photo

Curl help


  • Please log in to reply
5 replies to this topic

#1 KSquared

KSquared
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 18 October 2006 - 02:59 PM

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.serenocan...om/thankyou.htm

$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);

Why is this happening?

Any help would be MUCH appreciated.
Thanks,
Keith


#2 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 18 October 2006 - 03:07 PM

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

<?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";
}

?>

SA PHP Archives - www.phparchives.za.org

#3 KSquared

KSquared
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 18 October 2006 - 03:36 PM

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:

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

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?





#4 Destruction

Destruction
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 18 October 2006 - 04:01 PM

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

#5 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 18 October 2006 - 04:05 PM

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?


All the results from the page will be in $data. You can parse this for a result.
SA PHP Archives - www.phparchives.za.org

#6 KSquared

KSquared
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 18 October 2006 - 04:34 PM

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:

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

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






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users