【问题标题】:PHP CURL Post Fields is not working [closed]PHP CURL帖子字段不起作用[关闭]
【发布时间】:2023-03-13 07:26:01
【问题描述】:

PHP curl 帖子不起作用..

这是我的代码

   function post_to_url($url, $data) {
   $fields = '';
   foreach($data as $key => $value) { 
      $fields .= $key . '=' . $value . '&'; 
   }
   rtrim($fields, '&');

   $post = curl_init();

   curl_setopt($post, CURLOPT_URL, $url);
   curl_setopt($post, CURLOPT_POST, count($data));
   curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
   curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);

   $result = curl_exec($post);

   curl_close($post);
}

post_to_url("http://www.i-autosurf.com/signup.php",$data);

你能检查一下错误吗..

是代码有问题还是网站有什么不同。

【问题讨论】:

  • $fields 也可以是数组
  • 为了将来参考,添加您的错误并向您展示您已经尝试过的操作 - 不要只要求调试和代码
  • @learningpython 你可以阅读它here

标签: php curl


【解决方案1】:

您可以按原样传递带有字段的数组。试试

function post_to_url($url, $fields) {
    $post = curl_init($url);

    curl_setopt($post, CURLOPT_POST, true);
    curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($post);

    curl_close($post);
}

【讨论】:

  • 不工作,伙计!我没有收到确认邮件
  • $data = array("name" => "learningphp", "email1" => "anything@gmail.com", "email2" => "anything@gmail.com", "passwd " => "anything", "sitename" => "anything", "url" => "anything.com", "termscheck" => "0" );
  • 在 exec 之后显示 $result 中的内容。
  • 另外,“termscheck”应该是1
  • 结果中什么也没有,只有两三个空行
【解决方案2】:

试试这个

 $postfields = http_build_query($data);
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL,$url);
 curl_setopt($ch, CURLOPT_POST, true);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

【讨论】:

  • 不工作我没有收到确认邮件
  • 尝试设置此选项 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);如果这有帮助
猜你喜欢
  • 1970-01-01
  • 2013-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-24
  • 1970-01-01
相关资源
最近更新 更多