title: Curl connection refused port 80 and 443
body: <p>I have a function that I have deduced is failing specifically the curl_exec.
  the error response i get from curl is </p>
  
  <pre><code>    Failed to connect to www.domain.co.za port 80(and same for 443): Connection refused 
  </code></pre>
  
  <p>here is my function</p>
  
  <p>The weird thing is this stopped working only yesterday after being stable for months </p>
  
  <p>Can anyone point me in the right direction as to why this is failing?</p>
  
  <p>the url that it goes to is correct and works if entered in to the browser</p>
  
  <pre><code>    private function post_to_callback($url, $fields) {
          unset($fields-&gt;raw);
          //url-ify the data for the POST
          $fields_string = http_build_query($fields);
          //mail('anthony@domain.co.za','debug',$fields_string);
          //open connection
          $ch = curl_init();
  
          //set the url, number of POST vars, POST data
          // $count = 1;
          // $url = str_replace("https://", "http://", $url, $count);
          curl_setopt($ch,CURLOPT_URL, $url);
  
          //tried this and it didnt work
          curl_setopt( $ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  
          curl_setopt($ch,CURLOPT_POST, count($fields));
          curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
          //return the transfer as a string 
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  
  
      $result = curl_exec($ch);
      if(curl_errno($ch)){
          echo 'Curl error: ' . curl_error($ch);
  
          mail('anthony@domain.co.za','curl error','&lt;pre&gt;'. curl_error($ch) .'&lt;/pre&gt;');
      }
  
      //echo $result;
  
      //close connection
      curl_close($ch);
  }
  </code></pre>
  
  <p>EDIT: here is the script that it tries to curl to</p>
  
  <pre><code>&lt;?php
  mail('anthony@domain.co.za','debug','it got to the beginning of callback');
  $name = filter_input(INPUT_POST, 'full_name');
  $email = filter_input(INPUT_POST, 'email');
  $number = '0' . substr(str_replace('+', '', filter_input(INPUT_POST, 'phone_number')), -9);
  
  mail('anthony@d9omain.co.za','debug','it got to the campaign');
  
  $campaign = "Herbal Peel - Parkhurst - April 2018";
  $friendly = "Herbal%20Peel%20Special";
  $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
  $txt = "Last signup " . date('Y-m-d H:i:s');
  fwrite($myfile, $txt);
  foreach($_POST as $k =&gt; $p) {
  fwrite($myfile, "$k =&gt; $p \r\n");
  }
  fwrite($myfile, "$name $email $number $campaign");
  
  // Connect to foreign MySQL database
  $link = mysqli_connect('localhost', 'db', 'db', 'db');
  $query = "INSERT INTO cosmag_leads (campaign, name, number, email, date,friendly_name) VALUES ('$campaign', '$name', '$number', '$email', NOW(),'$friendly');";
  mail('anthony@domain.co.za','debug',$query);
  fwrite($myfile, $query);
  mysqli_query($link, $query);
  mysqli_close($link);
  </code></pre>