PHP使用cUrl提交表单

时间:2014-03-17 11:58:31

标签: php curl

我想使用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 :(得分: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.

答案 1 :(得分:0)

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

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