【问题标题】:php cUrl Error init errorphp cUrl Error 初始化错误
【发布时间】:2013-06-23 07:02:49
【问题描述】:

我正在尝试使用 curl,我认为这是 PHP 内置的很长时间了。

但我收到以下错误 Call to undefined function curl_init()

我从一个 ajax 教程中复制了这段代码,请谁能指出我正确的方向。

 $url = $_POST['url'];
 unset($_POST['url']);
 $fields_string = "";
 //url-ify the data for the POST
 foreach($_POST as $key=>$value) {
     $fields_string .= $key.'='.$value.'&';
 }
 $fields_string = rtrim($fields_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_POST,count($_POST));
 curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
 //execute post
 $result = curl_exec($ch);
 //close connection
 curl_close($ch);

【问题讨论】:

  • 你用的是什么php版本?
  • 在您的 php.ini 中启用 curl,或在此处或 google 上搜索 enable curl。您可能还需要安装它。
  • 您好 5.3.8 版,现在可以使用,但它认为缺少一个参数,但是当我手动复制到 URL 时一切正常?

标签: php ajax curl


【解决方案1】:

为什么不使用file_get_contents 而不是cURL

$url = (isset($_POST['url']) && is_string($_POST['url'])) ? $_POST['url'] : die('url parameter required.');
unset($_POST['url']);
$data = http_build_query($_POST, '', '&');
$result = file_get_contents($url, false, stream_context_create(array(
    'http' => array(
        'method' => 'POST',
        'header' => implode("\r\n", array(
            'Content-Type: application/x-www-form-urlencoded',
            'Content-Length: ' . strlen($data),
        )),
        'content' => $data,
    ),
)));

【讨论】:

  • 谢谢,只是尝试 file_get_contents 不会返回任何内容,我的客户端 jquery 会打开一个空白警报框。
猜你喜欢
  • 2016-09-03
  • 1970-01-01
  • 1970-01-01
  • 2017-04-05
  • 2015-03-18
  • 2019-08-05
  • 2013-04-12
  • 2018-08-27
  • 2015-04-06
相关资源
最近更新 更多