【问题标题】:enabling curl extension in php for 000webhost.com在 php 中为 000webhost.com 启用 curl 扩展
【发布时间】:2023-03-11 03:01:01
【问题描述】:

所以我正在尝试将我的应用程序与 facebook 集成,并且我使用默认 example.php 的代码作为我的 index.php。我已经更改了 appID 和 appSecret 以匹配我的应用程序的 id 和密码,每当我在 facebook 上测试代码时,它都会抛出一个异常,即 UnhandledCurlException。每当我调用 getUser() 方法时,用 try 和 catch 包装代码只会返回 0。

我知道我可以在 php.ini 上启用 curl 扩展,但我只能在我的本地主机上找到它,而不是在我用来部署我的应用程序的服务器 (000webhost.com) 上。

我还听说可以使用 .htaccess 文件来显式修改 php 配置,从而启用 curl 扩展,有人知道怎么做吗?还是我错过了其他任何选择?

这是引发错误的函数: 受保护的函数 makeRequest($url, $params, $ch=null) { 如果 (!$ch) { $ch = curl_init(); }

$opts = self::$CURL_OPTS;
if ($this->getFileUploadSupport()) {
  $opts[CURLOPT_POSTFIELDS] = $params;
} else {
  $opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');
}
$opts[CURLOPT_URL] = $url;

// disable the 'Expect: 100-continue' behaviour. This causes CURL to wait
// for 2 seconds if the server does not support this header.
if (isset($opts[CURLOPT_HTTPHEADER])) {
  $existing_headers = $opts[CURLOPT_HTTPHEADER];
  $existing_headers[] = 'Expect:';
  $opts[CURLOPT_HTTPHEADER] = $existing_headers;
} else {
  $opts[CURLOPT_HTTPHEADER] = array('Expect:');
}

curl_setopt_array($ch, $opts);
$result = curl_exec($ch);

$errno = curl_errno($ch);
// CURLE_SSL_CACERT || CURLE_SSL_CACERT_BADFILE
if ($errno == 60 || $errno == 77) {
  self::errorLog('Invalid or no certificate authority found, '.
                 'using bundled information');
  curl_setopt($ch, CURLOPT_CAINFO,
              dirname(__FILE__) . DIRECTORY_SEPARATOR . 'fb_ca_chain_bundle.crt');
  $result = curl_exec($ch); //the line 1003
}

// With dual stacked DNS responses, it's possible for a server to
// have IPv6 enabled but not have IPv6 connectivity.  If this is
// the case, curl will try IPv4 first and if that fails, then it will
// fall back to IPv6 and the error EHOSTUNREACH is returned by the
// operating system.
if ($result === false && empty($opts[CURLOPT_IPRESOLVE])) {
    $matches = array();
    $regex = '/Failed to connect to ([^:].*): Network is unreachable/';
    if (preg_match($regex, curl_error($ch), $matches)) {
      if (strlen(@inet_pton($matches[1])) === 16) {
        self::errorLog('Invalid IPv6 configuration on server, '.
                       'Please disable or get native IPv6 on your server.');
        self::$CURL_OPTS[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4;
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        $result = curl_exec($ch);
      }
    }
}

if ($result === false) {
  $e = new FacebookApiException(array(
    'error_code' => curl_errno($ch),
    'error' => array(
    'message' => curl_error($ch),
    'type' => 'CurlException',
    ),
  ));
  curl_close($ch);
  throw $e;
}
curl_close($ch);
return $result;
}

【问题讨论】:

    标签: php facebook .htaccess curl


    【解决方案1】:

    首先使用此代码确定 cURL 扩展是否真的被配置。

    <?php
    echo function_exists('curl_version') ? 1:0; // 1 = enabled , 0 = disabled.
    

    如果它被禁用,请让您的虚拟主机提供商启用该扩展程序。 [我认为这不是一件难事,而不是在 .htaccess 和其他手段在他们不知情的情况下启用cURL。 ]

    【讨论】:

    • 它返回 1,但是当我用我的本地主机测试时,它也返回 1(启用和禁用)。
    • (both enabled and disabled).我不明白。
    • 这意味着我用我的本地主机测试你的 php 代码,我测试了两次,第一次通过启用扩展和第二次禁用扩展(通过注释掉它),两个结果都返回 1。
    • 这是不可能的。也许您在启用/禁用扩展后忘记重新启动网络服务器。
    • 是的,你是对的,我忘了重启。现在似乎服务器启用了 curl,但支持的协议数量似乎远远少于我本地主机上支持的协议数量,9(服务器)到 19(本地主机),我不确定这是不是问题, "tftp","ftp","telnet","dict","ldap","http","file","https","ftps" 已启用。我想这应该不是问题。
    猜你喜欢
    • 2011-08-16
    • 2012-06-29
    • 2015-11-21
    • 2018-04-02
    • 2014-09-19
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 2016-09-14
    相关资源
    最近更新 更多