【问题标题】:Send HTTP Post with Array - PHP使用数组发送 HTTP Post - PHP
【发布时间】:2011-04-22 07:56:42
【问题描述】:

我正在尝试使用这个不错的功能:

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;
}

要将 POST 命令发送到特定的 url,我的问题是我试图以数组的形式发送 post 参数,类似于这样

login[username]: myusername
login[password]: mypassword,

但是我不能这样做,调用函数:

            $login_post = array('login[username]' => $email,
                            'login[password]' => '');
        do_post_request(getHost($website['code']), $login_post);

始终以以下形式将数据发送到帖子:

username: myusername
password: mypassword

如何避免这种情况(不使用 curl)?非常感谢。

谢谢

是啊

【问题讨论】:

  • 当您尝试以这种方式发送数据时会发生什么? var_dump[$_POST] 在接收方显示什么?
  • Zend 框架正在处理这个特定 url 上的 POST,并期望以 login[username]、login[password] 形式的数据,我正在尝试模拟
  • 啊。如果 ZF 使用这种方法,那么它一定是有可能做到的。删除了我的答案,否则
  • 我可以得到用户名:myusername,密码:mypassword,好像$data中的数组被扁平化了
  • 我明白了。我认为您必须传输一个真正的关联数组 array("username" => $email, "password" => "") 但如何在 curl 中执行此操作,我不知道

标签: php


【解决方案1】:

也许是这样?

<?php
// Define POST data

$donnees = array(
'login' => 'test',
'password' => '******' );

function http_build_headers( $headers ) {

       $headers_brut = '';

       foreach( $headers as $name => $value ) {
               $headers_brut .= $name . ': ' . $value . "\r\n";
       }

       return $headers_brut;
}

$content = http_build_query( $donnees );

// define headers

$headers = http_build_headers( array(
'Content-Type' => 'application/x-www-form-urlencoded',
'Content-Length' => strlen( $content) ) );

// Define context

$options = array( 'http' => array( 'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1) Gecko/20061010 Firefox/2.0',
'method' => 'POST',
'content' => $content,
'header' => $headers ) );

// Create context
$contexte = stream_context_create( $options );

// Send request
$return = file_get_contents( 'http://www.exemple.com', false, $contexte );
?>

【讨论】:

    【解决方案2】:
     $login_post = array(
    'login' => array(
    'username' => $email,
    'password' => ''))
    

    【讨论】:

      【解决方案3】:

      尝试使用stream_context_create

      $url = 'http://your_url.com/path';
      $data = array('login' => 'usrnname', 'password' => '**');
      
      $opt = array(
          'http' => array(
              'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
              'method'  => 'POST',
              'content' => http_build_query($data)
          )
      );
      $context  = stream_context_create($opt);
      $result = file_get_contents($url, false, $context);
      
      var_dump($result);
      

      此方法不使用 curl。

      【讨论】:

        猜你喜欢
        • 2012-02-25
        • 2010-12-12
        • 1970-01-01
        • 2012-01-03
        • 1970-01-01
        • 1970-01-01
        • 2018-07-01
        • 2011-02-25
        • 1970-01-01
        相关资源
        最近更新 更多