dsj2014 2017-02-23 10:04
浏览 70
已采纳

使用cURL将数据发布到外部URL

I want to use cURL to post some data to an external URL, here is the code they ask to embed, for some technical needs I need to create my own form action so I need cURL so POST those data: This is what I am asked to embed:

<form action='https://external.com/blabla' name=715000002374001 method='POST'>  
<input type='text'name='xnQsjsdp' value='c942f375287f707b73'/>  
<input type='hidden' name='zc_gad' id='zc_gad' value=''/> 
 <input type='text'name='xmIwtLD' value=1aff1efd0a4'/>  
<input type='text' name='actionType' value='TGVhZHM='/> 
<input type='text' name='returnURL' value='http&#x3a;&#x2f;&#x2f;www.blabla.de' />
</form>

I try to make it with cURL :

  $data = 'xnQsjsdp=c942f375287f707b73&xmIwtLD=TGVhZHM&actionType=TGVhZHM&returnURL&http&#x3a;&#x2f;&#x2f;www.blabla.de';
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL,'https://external.com/blabla');
  curl_setopt($ch, CURLOPT_URL,$url);
  //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data );
  $res = curl_exec ($ch);
  curl_close ($ch);

But seems not to be working !!!

  • 写回答

1条回答 默认 最新

  • doutu2017 2017-02-23 10:28
    关注

    You can use this class like

    class YourClass
    {
    
       private static $base_url = 'http://localhost/projects/';
       private static $key = 'your_key';
       private static $secret = 'your_secret';
    
       public static function yourMethodName($id = '', $fields = '', $is_return_transfer = 0)
       {
    
          if (is_array($fields))
          {
             $fields_str = http_build_query($fields, '', '&');
          } else
          {
             $fields_str = $fields;
          }
          $fields_str = $fields_str . "&key=" . self::$key . "&secret=" . self::$secret;
          $id_str = ($id != '') ? "/$id" : "";
          $url = self::$base_url . $id_str;
    
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_POST, count($fields));
          curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_str);
          if ($is_return_transfer == 1)
          {
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          }
          $result = curl_exec($ch);
          curl_close($ch);
          return $result;
       }
    
    }
    

    You can use this like YourClass::yourMethodName($id, $fields, $is_return_transfer);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器