PHP卷曲CURLOPT_IPRESOLVE

Til*_*ain 13 curl facebook ipv6 facebook-graph-api

我一直在使用Facebook图形API进行身份验证的facebook应用程序,最近facebook升级到IPv6,我的网络不支持IPv6,所以我的所有调用都开始返回Host无法访问错误,我在facebook上搜索了bug,发现我们仍然可以使用CURL CURLOPT_IPRESOLVE强制对facebook的请求使用IPv4.

现在,当我尝试使用curl向Facebook Graph API发送请求时,我得到通知:使用未定义的常量CURLOPT_IPRESOLVE - 假设为'CURLOPT_IPRESOLVE'

我想知道如何启用对此常量的支持,或者如何在php中禁用IPv6支持,以便我可以使用IPv4向Facebook Graph API发送请求.

我的代码是

    $url = ("https://graph.facebook.com/me/access_token?tokrn");
        $c = curl_init();
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_URL, $url);
        curl_setopt($c, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); 
        $contents = curl_exec($c);
        $err  = curl_getinfo($c,CURLINFO_HTTP_CODE);
        curl_close($c);
Run Code Online (Sandbox Code Playgroud)

谢谢

Ven*_*enu 12

检查卷曲版本

CURLOPT_IPRESOLVE自卷曲7.10.8起可用

试试这个示例代码进行测试

<?php

    $version = curl_version();

// These are the bitfields that can be used 
// to check for features in the curl build
$bitfields = Array(
            'CURL_VERSION_IPV6', 
            'CURLOPT_IPRESOLVE'
            );


foreach($bitfields as $feature)
{
    echo $feature . ($version['features'] & constant($feature) ? ' matches' : ' does not match');
    echo PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)

仅供参考:http://gcov.php.net/PHP_5_3/lcov_html/curl/interface.c.gcov.php

  • 仅供参考,同样重要的是要注意CURLOPT_IPRESOLVE和相关常量仅在PHP 5.3及更高版本中定义.如果您运行的是5.2,那么这些常量将无法访问,即使您的libcurl版本支持它(7.10.8,如@Venu指出的那样).另外,即使你手动定义常量(`define('CURLOPT_IPRESOLVE',113); define('CURL_IPRESOLVE_V4',1);`),PHP也会忽略它,你可以在`static int _php_curl_setopt()中看到它. Venu链接到源代码中的函数. (4认同)