Dave Juelz Dave Juelz - 1 month ago 13
PHP Question

cURL geto no response no error online server

Hope anyone can help me out with this. I have tried to call external site using cURL but I get no error or response. Yet it works fine on local server but does not work on production server. I had started the call using file_get_contents() but fail online too. I talked to the hosting and they mentioned the problem is within codes. here is my codes can anyone help!?`

function send_with_curl($url, $data) {
$data_string;
//url-ify the data for the POST
foreach ($data as $key => $value) { $data_string .= $key . '=' . $value . '&';
}
rtrim($data_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_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);

//execute post
$result = curl_exec($ch);
if (!is_string($result) || !strlen($result)) {
$result = "Failed to connect.";
}
//close connection
curl_close($ch);
return $result;
}


I also have another function that uses file_get_contents() and whichever I may use they work locally but fail online without an error I spent more than 4 hours trying to fix it with hosting guys until they finally said the error is within codes and they weren't familiar with those codes :(

function send_with_file($url, $context, $sender) {
global $database;
if (!$result = file_get_contents($url, false, $context)) {
echo "Message sending failed, please check your internet.";
exit ;
} else {
//--UPDATING THE DATABASE------
$sql_update = "SELECT * FROM users WHERE users_type='2'";
$query_update = $database -> query($sql_update);
while ($update = $database -> fetch_array($query_update)) {
$update_user = $update['users_id'];
if ($update_user == $sender) {

if ($result === FALSE) {/* Handle error */
}
} else {

}

}
}
return $result;
}

Answer

There was no problem with the codes, I just traced the url I was trying to connect to and realized it takes too long to communicate with the hosting server.