2

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;
}
David NIWEWE
  • 117
  • 1
  • 4
  • 13

2 Answers2

0

Compare $result against false and then check curl_error()

Something like...

$result = curl_exec($ch);
if($result === false) {
    echo "Error in cURL : " . curl_error($ch);
}
Dale
  • 10,384
  • 21
  • 34
  • it doesn't give any response! :'( – David NIWEWE May 05 '16 at 14:17
  • Are you sure the URL is actually returning anything? – Dale May 05 '16 at 14:29
  • pretty sure, cause on my local server I get the response from the URL! but on production server I get nothing and it fails to do the request – David NIWEWE May 05 '16 at 14:31
  • Mind sharing said URL? – Dale May 05 '16 at 14:32
  • http://197.243.19.159:8080/staronly/rapid.htm that is the url without passing any parameter it will return an error. but that error I don't even get from the hosting server but locally I get it! – David NIWEWE May 05 '16 at 14:49
  • hmmm.. I can access that URL just fine.. it says invalid client but still.. will think on it some more... is your server on amazon aws by chance... maybe getting ip blocked? (just trying to eliminate things) – Dale May 05 '16 at 14:54
  • nope it's on hostoople, it's so frustrating I spend more than 10hours on this thing without any clue of what wrong on the server or my codes. but so far my codes are fine as they work effectively locally but I'm wondering whether there is any thing I can do make them online – David NIWEWE May 05 '16 at 15:06
  • try this as a quick sanity check `echo file_get_contents('http://197.243.19.159:8080/staronly/rapid.htm');` do you get anything? – Dale May 05 '16 at 15:07
  • On my local server I get this Invalid clientName but online nothing! – David NIWEWE May 05 '16 at 15:26
  • I'm at work at the moment but I'll think on it and try some ideas when I'm home – Dale May 05 '16 at 15:38
0

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.

David NIWEWE
  • 117
  • 1
  • 4
  • 13