PHP Curl Extension无法正常工作

时间:2011-06-01 14:44:55

标签: php .htaccess curl

当我在本地主机上试用它时,我写了一篇正确为我提取数据的文章。它在另一台服务器上运行良好。但是当我把它转移到服务器时,它会显示警告信息......

Warning: (null)(): 4 is not a valid cURL handle resource in Unknown on line 0

任何人都可以建议我必须在.htacess中进行哪些更改以及我需要在控制面板中进行更改...

以下是我正在使用的一段代码。

function multiRequest($data, $options = array()) {

  // array of curl handles
  $curly = array();
  // data to be returned
  $result = array();

  // multi handle
  $mh = curl_multi_init();

  // loop through $data and create curl handles
  // then add them to the multi-handle
  foreach ($data as $id => $d) {
$curly[$id] = curl_init();

$url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
curl_setopt($curly[$id], CURLOPT_URL,            $url);
curl_setopt($curly[$id], CURLOPT_HEADER,         0);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);

// post?
if (is_array($d)) {
  if (!empty($d['post'])) {
    curl_setopt($curly[$id], CURLOPT_POST,       1);
    curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
  }
}

// extra options?
if (!empty($options)) {
  curl_setopt_array($curly[$id], $options);
}

curl_multi_add_handle($mh, $curly[$id]);
  }

  // execute the handles
  $running = null;
  do {
    curl_multi_exec($mh, $running);
  } while($running > 0);

  // get content and remove handles
  foreach($curly as $id => $c) {
    $result[$id] = curl_multi_getcontent($c);
    curl_multi_remove_handle($mh, $c);
  }

  // all done
  curl_multi_close($mh);

  return $result;
}

for ($i=0;$i<$length;$i++){
$no = $start + $i;
$data[$i]['url']  = 'http://abc.php';
$data[$i]['post'] = array();
$data[$i]['post']['regno']   = $no;
}

$r = multiRequest($data);

// the I have a code to use the $r (result array obtained)

由于

1 个答案:

答案 0 :(得分:1)

这并不意味着cURL扩展不起作用,恰恰相反。 CURL正在工作,但是当您尝试访问传递给curl的选项时,您没有传递curl资源处理程序。

例如,

 $ch = curl_init(); 
 curl_setopt($ch, CURLOPT_URL, $url); 

你需要使用$ ch作为你的处理程序,我想你不会这样做?

作为旁注,如果没有安装curl,你会得到一个例外。除非你在try catch / function中包装它。