首页 文章

PHP使用cUrl提交表单

提问于
浏览
0

我想通过 cUrlthis site提交任何IP地址 . 我的cUrl选项无法正常工作:

$h = curl_init();

  curl_setopt($h, CURLOPT_URL, "http://www.whatismyip.com/ip-whois-lookup/"); 
  curl_setopt($h, CURLOPT_POST, true);
  curl_setopt($h, CURLOPT_POSTFIELDS, array(
  'IP' => '2.179.144.117',
  'submitted' => 'submitted'
  ));
  curl_setopt($h, CURLOPT_HEADER, false);
  curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);

  $result = curl_exec($h);
  echo $result;

2 回答

  • 0

    快速检查发送到该网页的 Headers ,显示“已提交”的帖子变量应设置为true,而不是提交 .

    Remark :请注意,whatismyip.com可能不允许通过抓取访问其工具 .

  • 1

    使用 http_build_query() 函数为您的数组数据提供,以便可以在请求的IP上正确解码:

    curl_setopt($h, CURLOPT_POSTFIELDS, http_build_query(
       array(
          'IP' => '2.179.144.117',
          'submitted' => 'submitted'
       )
    ));
    // echo http_build_query(array('IP' => '2.179.144.117', 'submitted' => 'submitted'));
    // outputs: IP=2.179.144.117&submitted=submitted and this will be added to the end of
    // the requested URL in GET request.
    

相关问题