为了账号安全,请及时绑定邮箱和手机立即绑定

如何用 curl 替换具有 3 个参数的 file_get_contents

如何用 curl 替换具有 3 个参数的 file_get_contents

PHP
绝地无双 2021-11-13 10:31:00
我正在尝试在我的网站上配置 google reCAPTCHA v3,并且由于 https,我没有使用 file_get_contents 得到响应。我需要用 curl 替换它,但我不知道如何使用 3 个参数来实现它。$url="googleverify.com";$options = array(        'ssl' => array(          'verify_peer'=>false,          'verify_peer_name'=>false,          'header'  => "Content-type: application/x-www-form-urlencoded\r\n",          'method'  => 'POST',          'content' => http_build_query($data)        )      );    $context  = stream_context_create($options);    $response = file_get_contents($url, false, $context);
查看完整描述

2 回答

?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

这修复了错误....


$context  = stream_context_create($options);


$ch = curl_init();

curl_setopt( $ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_HTTPHEADER, $options);

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

$response = curl_exec($ch);


  print_r($response);


查看完整回答
反对 回复 2021-11-13
?
万千封印

TA贡献1891条经验 获得超3个赞

   'verify_peer'=>false,

      'verify_peer_name'=>false,

翻译成


curl_setopt_array($ch,array(

    CURLOPT_SSL_VERIFYPEER=>false,

    CURLOPT_SSL_VERIFYHOST=>0,

));


      'header'  => "Content-type: application/x-www-form-urlencoded\r\n",

翻译成


curl_setopt_array($ch,array(

    CURLOPT_HTTPHEADER=>array(

        "Content-type: application/x-www-form-urlencoded",

    )

));

(并且不,不要将 \r\n 与 CURLOPT_HTTPHEADER 一起使用)和


     'method'  => 'POST',

翻译成


curl_setopt($ch,CURLOPT_POST,true);


  'content' => http_build_query($data)

翻译成


curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($data));

最后,把它们放在一起,我们得到:


curl_setopt_array($ch,array(

    CURLOPT_SSL_VERIFYPEER=>false,

    CURLOPT_SSL_VERIFYHOST=>0,

    CURLOPT_HTTPHEADER => array(

        "Content-type: application/x-www-form-urlencoded"

    ),

    CURLOPT_POST=>true,

    CURLOPT_POSTFIELDS=>http_build_query($data)

));


查看完整回答
反对 回复 2021-11-13
  • 2 回答
  • 0 关注
  • 121 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信