tags:

views:

30

answers:

2

Hi everyone

I have recently changed servers, and one of my scripts is not functioning on the new server, as fopen is not enabled?

Is it possible to change the following code to use the CURL function instead?

Hope someone can help!

<?php
$postToFileName = 'http://www.somesite.com/postfile.aspx'; 
$postArr = array(

'NM' => $row['Lead_Name'],

'EM' => $row['Lead_Email'],

'PH' => $row['Lead_Tel'],

);

$opts = array(

'http'=>array(

'method' => 'POST',

'header' => "Content-type: application/x-www-form-urlencoded\r\n",

'content' => http_build_query($postArr)

)

);

$context = stream_context_create($opts);

$fp = fopen($postToFileName, 'r', false, $context);

$returnedMessage = '';

while (!feof($fp)) {

$returnedMessage .= fgets($fp);

}

fclose($fp);

if ($returnedMessage == '') {

$returnedMessage = 'No Message';

} else if (strlen($returnedMessage) > 250) {

$returnedMessage = substr($returnedMessage,0,250);

}

$returnedMessage = preg_replace("/[\r\n]/", '', $returnedMessage);

$returnedMessage = mysql_real_escape_string($returnedMessage, $sql);

$q = "UPDATE leads SET Data_Sent = '$returnedMessage' WHERE Lead_ID = $id";

mysql_query($q, $sql);

array_push($leadsSent, $id);

}

}

mysql_close($sql);

return $leadsSent;

}



?>
A: 

Yes, it's quite possible. See the examples here: http://us.php.net/manual/en/curl.examples-basic.php

Scott Saunders
A: 

Sure. It should probably look like this (not tested of course, might need some minor changes) :

$postToFileName = 'http://www.somesite.com/postfile.aspx'; 
$postArr = array(
    'NM' => $row['Lead_Name'],
    'EM' => $row['Lead_Email'],
    'PH' => $row['Lead_Tel'],
);

$ch = curl_init($postToFileName);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$returnedMessage = curl_exec($ch);
curl_close($ch);
wimvds