dsj2014 2017-02-23 02: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:

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

I try to make it with cURL :

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

But seems not to be working !!!

  • 写回答

1条回答 默认 最新

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

    You can use this class like

    1. class YourClass
    2. {
    3. private static $base_url = 'http://localhost/projects/';
    4. private static $key = 'your_key';
    5. private static $secret = 'your_secret';
    6. public static function yourMethodName($id = '', $fields = '', $is_return_transfer = 0)
    7. {
    8. if (is_array($fields))
    9. {
    10. $fields_str = http_build_query($fields, '', '&');
    11. } else
    12. {
    13. $fields_str = $fields;
    14. }
    15. $fields_str = $fields_str . "&key=" . self::$key . "&secret=" . self::$secret;
    16. $id_str = ($id != '') ? "/$id" : "";
    17. $url = self::$base_url . $id_str;
    18. $ch = curl_init();
    19. curl_setopt($ch, CURLOPT_URL, $url);
    20. curl_setopt($ch, CURLOPT_POST, count($fields));
    21. curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_str);
    22. if ($is_return_transfer == 1)
    23. {
    24. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    25. }
    26. $result = curl_exec($ch);
    27. curl_close($ch);
    28. return $result;
    29. }
    30. }

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

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部