0

I grabbed the function curl_request_async from How do I make an asynchronous GET request in PHP?

 // $type must equal 'GET' or 'POST'
function curl_request_async($url, $params, $type='POST')
{
  foreach ($params as $key => &$val) {
    if (is_array($val)) $val = implode(',', $val);
    $post_params[] = $key.'='.urlencode($val);
  }
  $post_string = implode('&', $post_params);

  $parts=parse_url($url);

  $fp = fsockopen($parts['host'],
      isset($parts['port'])?$parts['port']:80,
      $errno, $errstr, 30);

  // Data goes in the path for a GET request
  if('GET' == $type) $parts['path'] .= '?'.$post_string;

  $out = "$type ".$parts['path']." HTTP/1.1\r\n";
  $out.= "Host: ".$parts['host']."\r\n";
  $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
  $out.= "Content-Length: ".strlen($post_string)."\r\n";
  $out.= "Connection: Close\r\n\r\n";
  // Data goes in the request body for a POST request
  if ('POST' == $type && isset($post_string)) $out.= $post_string;

  fwrite($fp, $out);
  fclose($fp);
}

It worked perfectly with my previous host but after I moved to a new host, it just does not work. I already installed php-curl but it's like the function just does not call the url.

There was no error given too. Please help I have been stuck with this for days.

Community
  • 1
  • 1
Boy
  • 405
  • 2
  • 9
  • 21
  • call phpinfo and check if allow_url_fopen is enabled http://www.php.net/manual/de/filesystem.configuration.php – roman Feb 23 '14 at 21:59
  • well for starters, this function doesn't use curl, it uses a socket – CrayonViolent Feb 23 '14 at 22:00
  • @roman, yes 'allow_url_fopen' is On in phpinfo(). – Boy Feb 23 '14 at 22:31
  • Could anyone test this function. I would like to make sure that's not a problem with my server. It worked with my previous host tho. – Boy Feb 23 '14 at 22:35
  • If I cannot really use 'curl_request_async' function, are there any other similar thing? I want to send multiple GET/POST at a time without waiting for response. – Boy Feb 24 '14 at 00:01

1 Answers1

0

One thing you could verify is if you can call fsockopen on your server, the code you posted does not use curl, but try to directly open a socket

ICY382
  • 26
  • 1
  • I tried to use a script to test whether fsockopen works or not by following this http://madmakz.com/code/pub/fsockopen/. It seems like fsockopen is working on my host but still the function I mentioned just does not work :( – Boy Feb 23 '14 at 22:33
  • i tested the function on my server and it's sending data correctly, but this function does not output anything. When you say it's not working, do you mean that you don't receive the call on the script you're calling ? Do you have any value in the $errno and $errstr after your call ? – ICY382 Feb 23 '14 at 23:21
  • $errno returns 0 and $errstr is empty. I know it does not output anything but the URL that I input to this function should create a DB record but when I use this function, no record added in DB. Once I copy and paste the exact URL and run it on browser, record is added on DB. That's why I think this function does not really call the URL I specified. – Boy Feb 23 '14 at 23:51
  • Moreover, I let my friend to have exactly the same code and run. It works on his machine but not my server. Really desperate :( – Boy Feb 23 '14 at 23:53
  • do you know if your host run php in safe mode ? – ICY382 Feb 24 '14 at 00:20