dsfds4551
2015-10-17 07:22
浏览 177
已采纳

Http post请求使用fopen

In the first file - the file I execute I have the following content:

<?php   
$name = "Julia";
$article = "I like papers";
$url = "http://domain.com/process.php";
$param = array('http' => array(
          'method' => 'POST',
          'content' => $article

        ));

$mad = @stream_context_create($param);
$fp = @fopen($url, 'rb', false, $mad);
$response = @stream_get_contents($fp);
echo $response;
?>

in the second file http://domain.com/process.php I have this:

<?php
$name = $_POST["name"];
$article = $_POST["content"];
$article = $_POST["article"];

echo $article;
echo $name;
echo "Hello there</br>:
";

?>

The output that I get is just:

 "Hello there"

So what is wrong, how do I pass the values $article and $name via the request and how to I extract them in the file process.php?

图片转代码服务由CSDN问答提供 功能建议

在第一个文件中 - 我执行的文件我有以下内容: < pre> &lt;?php $ name =“Julia”; $ article =“我喜欢论文”; $ url =“http://domain.com/process.php"; $ param = array('http'=&gt; array( 'method'=&gt;'POST', 'content'=&gt; $ article )); $ mad = @ stream_context_create($ param); $ fp = @fopen($ url,'rb',false,$ mad); $ response = @stream_get_contents($ fp); echo $ response; ?&gt; \ n

在第二个文件 http://domain.com /process.php 我有这个:

 &lt;?php 
 $ name = $ _POST [“name”]; 
 $ article = $ _POST  [“content”]; 
 $ article = $ _POST [“article”]; 
 
echo $ article; 
echo $ name; 
echo“Hello there&lt; / br&gt;:
”; 
 
?  &gt; 
   
 
 

我得到的输出只是:

 “Hello there”
   
 
 

所以有什么问题,我如何传递$ article和$的值 通过请求命名以及如何在文件process.php? 中提取它们

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongsimu4422 2015-10-17 08:03
    已采纳

    You got it wrong at the content part, use http_build_query() to build the POST query.

    $param = array( 'http' => array('method' => 'POST',
                                    'header' => "Content-type: application/x-www-form-urlencoded
    ",
                                    'content' => http_build_query($data)
                                    ));
    

    And your post parameters should be like below, where the input name as key

    $data['name'] = 'Julia';
    $data['article'] = 'I like papers';
    

    Personally, I will use curl.

    打赏 评论

相关推荐 更多相似问题