【问题标题】:PHP+cURL+POST not workingPHP+cURL+POST 不工作
【发布时间】:2017-03-15 12:50:06
【问题描述】:

我正在尝试向网站发出帖子请求,但它不起作用。

我已经使用 Postman(Chrome 扩展程序)发出了 POST 请求,它工作正常。

我想要获取的网址类似于:https://scanurl.net/?u=google.com

我的代码是:

    <?php
    $ch = curl_init();

    $postvars = [
    'u' => 'google.com'
    ];

  $url = "https://scanurl.net/";

  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_POST, 1);                //0 for a get request
  curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3);
  curl_setopt($ch,CURLOPT_TIMEOUT, 20);
  $response = curl_exec($ch);
  print "curl response is:" . $response;
  curl_close ($ch);
?>

解决方法(使用POSTMAN代码sn-ps:

<?php

$curl = curl_init();

$url='https://scanurl.net/?u=gxxoe.com';
  curl_setopt($curl,CURLOPT_URL,$url);

curl_setopt_array($curl, array(
 // CURLOPT_URL => "https://scanurl.net/?u=gxoe.com",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "postman-token: be8b2f15-e8fd-86f9-5d7a-01f4b4b79587"
  ),
));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

?>

【问题讨论】:

  • 错误是什么?您对请求有何期望?
  • 我什么也没得到。我希望得到这个网站的内容:scanurl.net/?u=google.com

标签: php curl http-post


【解决方案1】:

您可以直接从 Postman 生成 curl-request,但请记住,这里有一个小错误: https://github.com/postmanlabs/postman-app-support/issues/2631

您必须添加参数CURLOPT_CUSTOMREQUEST 和值POST,然后必须添加您的CURLOPT_POSTFIELDS

【讨论】:

    【解决方案2】:

    它可能在 Postman 中有效,因为您设置的是 GET 变量而不是不同的 POST 变量。我已将上面的代码修改为应该可以使用的代码:

    <?php
      $ch = curl_init();
      $url = "https://scanurl.net/";
    
      curl_setopt($ch,CURLOPT_URL,$url.'?u=google.com');
      curl_setopt($ch,CURLOPT_POST, 0);
      curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3);
      curl_setopt($ch,CURLOPT_TIMEOUT, 20);
      $response = curl_exec($ch);
      print "curl response is:" . $response;
      curl_close ($ch);
    ?>
    

    编辑:我应该补充一点,在经常这样做之前,请确保您与网站所有者核实,因为他们可能不会善待从他们的网站上抓取信息的人。

    【讨论】:

    • 如您所见,这是一个 POST 表单:
      scanurl.net/index.php" target="_top" method="post" name="uef" class="uef" style=" " align="center" accept-charset="UTF-8">
    • 你试过我提供的代码了吗?我在发布之前对其进行了测试:)
    • 使用邮递员和 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false) ,我已经解决了这个问题。谢谢
    【解决方案3】:

    试试这个,它很慢,需要 3-5 分钟导致重定向,但应该可以取回数据。

    $url = "https://scanurl.net/";
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, "u=google.com");   
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/x-www-form-urlencoded",'Content-Length: ' . strlen("u=google.com")  )  );
    $result=curl_exec($ch);
    curl_close($ch);
    var_dump($result);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      相关资源
      最近更新 更多