douyi5157 2013-02-19 06:01
浏览 8
已采纳

使用php发布数据[重复]

This question already has an answer here:

I want to know how to post data with php, for example get the input from the local php then write to the input with id on another webpage. I think that those are called PHP bots, but I didn't quite understand the function. If you could just show me the code, if this is possible.

UPDATE: To make it cleaner - how to "write" to an input on another page with PHP.

</div>
  • 写回答

1条回答 默认 最新

  • dtkf64283 2013-02-19 06:08
    关注

    To Post Data to an URL, you can use CURL for that.

    $my_url = 'http://www.google.com';
    $post_vars = 'postvar1=' . $postvar1 . '&postvar2=' . $postvar2;
    
    $curl = curl_init($my_url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_vars);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    
    $response = curl_exec($curl);
    

    UPDATE

    If you don´t want to use CURL you can try it this way:

    $server= 'www.someserver.com';
    $url = '/path/to/file.php';
    $content = 'name1=value1&name2=value2';
    $content_length = strlen($content);
    $headers= "POST $url HTTP/1.0
    Content-type: text/html
    Host: $server
    Content-length: $content_length
    
    ";
    $fp = fsockopen($server, $port, $errno, $errstr);
    if (!$fp) return false;
    fputs($fp, $headers);
    fputs($fp, $content);
    $ret = "";
    while (!feof($fp)) {
    $ret.= fgets($fp, 1024);
    }
    fclose($fp);
    print $ret;
    

    and if you´re using PHP5 here's a function you can use to post data to an URL:

    function do_post_request($url, $data, $optional_headers = null)
    {
      $params = array('http' => array(
                  'method' => 'POST',
                  'content' => $data
                ));
      if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
      }
      $ctx = stream_context_create($params);
      $fp = @fopen($url, 'rb', false, $ctx);
      if (!$fp) {
        throw new Exception("Problem with $url, $php_errormsg");
      }
      $response = @stream_get_contents($fp);
      if ($response === false) {
        throw new Exception("Problem reading data from $url, $php_errormsg");
      }
      return $response;
    }
    

    this function is from here

    and to use this function do something like

    <?php
    
    do_post_request('http://search.yahoo.com/', 'p=hello+yahoo')
    
    ?>
    

    this will search for "hello yahoo"

    展开全部

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

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

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

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

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

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

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

客服 返回
顶部