View Full Version : Any PHP Experts In Here ... Need Some HELP!
sess4561
11th August 2010, 12:27
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;
}
?>
utilitiessavings
11th August 2010, 12:51
Hi, I'm a PHP coder but no cURL expert I'm afraid, so I would offer you this advice...
add comments to every line saying what it does
take the extra braces out of the bottom - copy and paste error?
post your question on stackoverflow.com, it's a really great site and you should get a quick answer
Is it just an AJAX POST to a specific url? Finding it hard to work out what your code is doing tbh, but I have never really used fopen apart from copy and modify jobs...
Also, can you not just get your web host to change your settings? Or do it using php.ini?
I am posting a generic cURL function below... similar to something I use. I just wouldn't know how to convert your code to use this I'm afraid,
// takes $fields array as input (in my case a $_POST array)
function add_to_somewhere($fields)
{
$url = 'https://www.theurlyouwanttopostto.com/';
$fields_string = "";
//url-ify the data for the POST
foreach($fields as $key => $value)
{
$fields_string .= $key . '=' . stripslashes($value) . '&';
}
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, TRUE);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
}
Feel free to ask me anything else and I'll do my best to help.
Sorry I can't be of more help. GOOD LUCK!
awebapart.com
11th August 2010, 13:08
Just out of interest, which hosting company has disabled fopen in their PHP configuration?
It looks like your code is attempting an fopen with a http url. It might be the case that this has been disabled but fopen with an internal filename is possible. I'm assuming the file you want to write to is internal, i.e. sitting on the same website you are running the script, in which case the http part isnt necessary, just use a local filename and directory.
You may also have to check owner priviliges of the php script itself and the files/directories.
Failing that there might be other simple options like file_get_contents or file_put_contents.
EDIT: Looking closer at your code, it looks like fopen might not be the ideal method anyway, since it looks like you are not really wanting to open a file, but rather request a web page, possibly with a HTTP post! so strike what I said.
awebapart.com
11th August 2010, 14:20
To add to utilitiessaving's example, and looking at the php curl examples (http://php.net/manual/en/curl.examples.php), I would also add:
urlencode() around the stripslashes($value)
return $result; at the end of the function
I'm not sure though whether the function return value would be the equivalent of the value of $returnedMessage in the original script.
awebapart.com
11th August 2010, 14:31
I'm not sure though whether the function return value would be the equivalent of the value of $returnedMessage in the original script.
Just checked and the return value of curl_exec by default is TRUE or FALSE, unless the CURLOPT_RETURNTRANSFER option is set to TRUE, which it is in that example, so it should return the requested page.
edmondscommerce
11th August 2010, 14:34
you might find this blog article useful here:
http://www.edmondscommerce.co.uk/firefox/building-spiders-grab-data-post-forms-and-interact-with-web-sites-automatically/
your host hasn't disabled fopen (that's not really possible), what they have disabled is allow_url_fopen
Its pretty common to find that disabled, there are security issues with it, especially in shared hosting environments.
Curl is a much better choice for this kind of task anyway so being forced to use it isn't necessarily a bad thing.