cURL oddities
Posted by: jjustian (---.c3-0.sbo-ubr2.sbo.ma.static.cable.rcn.com)
Date: June 23, 2008 07:15PM

Hello,


I am relatively new to WAMP but am an intermediate level developer. I am trying to have cURL call to a website using GET with a querystring. My final code says 'echo $res' ... $res is the resulting html response.

However, all I get is a blank page. I tried this on a production box... scary, just because I knew it had a good build out and wanted to see if it was environment or code. The script ran and returned the results I expected.

Does anyone know why cURL in WAMP server doesn't send the results back to the browser?

Could be a simple config issue or an issue with the extendion... I'm clueless.

Thanks for your help, here's the sample code:

$post = "my querystring";
$r = curl_init('URL of the Get request without the args');
curl_setopt($r, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($r);
curl_close($r);
echo $res;

Options: ReplyQuote
Re: cURL oddities
Posted by: stevenmartin99 (---.b-ras1.blp.dublin.eircom.net)
Date: June 23, 2008 07:31PM

$post = "my querystring";
$r = curl_init('URL of the Get request without the args');
curl_setopt($r, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($r);
curl_close($r);
echo $res;



not looking into this code but straight away


$post = "my querystring"; this wont do anything but set $post to it

u need

$variblename = $_POST['my querystring']

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: cURL oddities
Posted by: jjustian (---.c3-0.sbo-ubr2.sbo.ma.static.cable.rcn.com)
Date: June 23, 2008 08:06PM

Thanks for the input. I just stripped out my actual query string from the variable because it has values I don't want posted. I should have written:

$post = "somequeryname=somequeryvalue&someotherqueryname=someotherqueryvalue";

Options: ReplyQuote
Re: cURL oddities
Posted by: toivo (203.19.130.---)
Date: June 25, 2008 01:30AM

Hi,

Here is an example from a working application:

// may not be needed
$user_agent = ' Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12';

$form_vars = array();
$form_vars['FirstName'] = $first_name;
$form_vars['LastName'] = $last_name;
$form_vars['Company'] = $company;
$form_vars['Country'] = $country;
$form_vars['Email'] = $email;

$post_data = '';
foreach ($form_vars as $var => $value) {
if (trim($value)) {
$post_data .= urlencode($var).'='.urlencode($value).'&';
}
}
// drop last &
$post_data = substr($post_data, 0, -1);

// get curl handle
$ch = curl_init();
if ($ch === false) {
error_log('Unable to get a curl handle');
}

curl_setopt($ch, CURLOPT_URL, $my_url);
curl_setopt($ch, CURLOPT_REFERER, 'http://example.com');
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); // may not be needed
curl_setopt($ch, CURLOPT_POST, 1); // POST
curl_setopt($ch, CURLOPT_HEADER, 0); // do not return headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // causes an error in eval()

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // contents of POST

$curl_result = @curl_exec($ch);
$errnum = curl_errno($ch);

if ($errnum != 0) {
$curl_error = curl_error($ch);
error_log($my_url." status: ".$errnum." - ".$curl_error);
}
curl_close($ch);


A couple of tips:

If your request needs to go through a proxy, you need to include the details:

curl_setopt($ch, CURLOPT_PROXY, '192.168.2.99'); // proxy
curl_setopt($ch, CURLOPT_PROXYPORT, 3128); // proxy port

I noticed that if the code is run through eval(), this change wouldmake it work:


//$set_transfer = CURLOPT_RETURNTRANSFER; // constant causes error in curl_setopt
$set_transfer = 19913;
curl_setopt($ch, $set_transfer, 1); // transfer the return as string

Regards,

toivo
Sydney, Australia

Options: ReplyQuote


Sorry, only registered users may post in this forum.