【问题标题】:file_get_contents() and Curlfile_get_contents() 和卷曲
【发布时间】:2015-12-13 12:33:18
【问题描述】:

我在我的 laravel 4.2 代码中使用 file_get_contents() php 函数在通过 facebook 登录后获取个人资料照片。

当我使用它时它工作正常

$arrContextOptions=array(
    "ssl"=>array(
        "verify_peer"=> false,
        "verify_peer_name"=> false,
    ),
);
$content = file_get_contents($myurl, false, stream_context_create($arrContextOptions));

但它在系统中制造了一个安全漏洞,正如之前在同一问题的另一个问题中提到的那样, 如果我不使用这种安全漏洞方法,我的脸上就会出现错误,我无法处理

错误异常 (E_WARNING) 帮助 file_get_contents():SSL 操作失败,代码为 1。OpenSSL 错误消息:错误:14090086:SSL 例程:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed"

然后我尝试了Curl方法

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$myURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$content = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $content);

它没有出错,也没有“没有照片返回”! 那么如何以安全干净的方式获取个人资料照片并使用 php(laravel 4.2)保存它?! 我正在使用 XAmpp 在我的本地主机上测试它

【问题讨论】:

  • 可能添加关注位置,如此处所述:stackoverflow.com/questions/9680676/… curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  • 刚刚试了一下,Curl 什么都不返回,没有编译错误!谢谢
  • 响应的状态码是什么?您可以添加: curl_setopt($ch, CURLOPT_HEADER, true);并 echo $content 查看响应的标头是什么,它可以帮助发现问题。

标签: php security ssl curl


【解决方案1】:

工作代码:

    $user_id = PUT_USER_ID_HERE;
    $url = 'http://graph.facebook.com/' . $user_id . '/picture?type=large';

    $finalUrl = get_final_url($url);


    $path = 'img/pic_facebook.jpg';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$finalUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $content = curl_exec($ch);
    curl_close($ch);
    file_put_contents($path, $content);


function get_final_url( $url, $timeout = 5 )
{
    $url = str_replace( "&", "&", urldecode(trim($url)) );

    $cookie = tempnam ("/tmp", "CURLCOOKIE");
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $ch, CURLOPT_ENCODING, "" );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
    $content = curl_exec( $ch );
    $response = curl_getinfo( $ch );
    curl_close ( $ch );

    if ($response['http_code'] == 301 || $response['http_code'] == 302)
    {
        ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
        $headers = get_headers($response['url']);

        $location = "";
        foreach( $headers as $value )
        {
            if ( substr( strtolower($value), 0, 9 ) == "location:" )
                return get_final_url( trim( substr( $value, 9, strlen($value) ) ) );
        }
    }

    if (    preg_match("/window\.location\.replace\('(.*)'\)/i", $content, $value) ||
            preg_match("/window\.location\=\"(.*)\"/i", $content, $value)
    )
    {
        return get_final_url ( $value[1] );
    }
    else
    {
        return $response['url'];
    }
}

函数取自这里:PHP Curl following redirects

【讨论】:

  • 它生成旧错误 get_headers(): SSL operation failed with code 1. OpenSSL 错误消息:error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed 感谢@Pmaniora 尝试帮助
  • 我现在也试过了 stackoverflow.com/questions/6400300/… ,但仍然是同样的旧错误。 SSL 操作失败,代码为 1。OpenSSL 错误消息:错误:14090086:SSL 例程:SSL3_GET_SERVER_CERTIFICATE:证书验证失败
猜你喜欢
  • 2023-03-19
  • 2010-12-10
  • 2010-12-24
  • 2018-05-02
  • 2011-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多