【问题标题】:Curl not passing the value to the php file卷曲没有将值传递给 php 文件
【发布时间】:2014-05-09 05:51:28
【问题描述】:

我有一个名为 c.php 的文件,我在其中尝试将 var name 的值传递给 cu.php 进行处理,但我得到的只是一个空白屏幕:/ 它是一个错误还是一个空值正在通过

而c.php的内容是

 <?php
    $name = " hello " ; 
    $post= 'name = '.urlencode($name);

    $ch =curl_init();
    curl_setopt($ch , CURLOPT_URL , 'http://www.xxxxxxx.com/cu.php');
    curl_setopt($ch, CURLOPT_POST ,TRUE ) ;
    curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
    $r = curl_exec($ch);
    curl_close($ch);
    ?>        

而cu.php的内容是

   <?php

    $n = $_POST['name'];
    echo $n ;
    ?>

【问题讨论】:

  • 哪个文件包含什么?此外,您发布的两个文件都命名为 cu.php。很难跟上。
  • @David 很抱歉第一个文件是 c.php 而不是 cu.php

标签: php html post curl


【解决方案1】:

您需要echo 结果并像这样传递参数..

<?php
$name = " hello ";
$post="name=$name"; //<-- Simpler way
$ch =curl_init();
curl_setopt($ch, CURLOPT_URL , 'http://www.xxxxxxx.com/cu.php');
curl_setopt($ch,CURLOPT_POST ,TRUE ) ;
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
echo $r = curl_exec($ch);  //<---- echo here
curl_close($ch);
?>

【讨论】:

    【解决方案2】:

    将帖子字段设置为这样的数组:

    <?php
    $name = " hello ";
    
    $post = array(
        'name' => urlencode($name),
    );
    
    $ch =curl_init();
    curl_setopt($ch, CURLOPT_URL , 'http://www.xxxxxxx.com/cu.php');
    curl_setopt($ch,CURLOPT_POST ,TRUE ) ;
    curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
    echo $r = curl_exec($ch);
    curl_close($ch);
    ?>
    

    【讨论】:

    • 感谢它现在的工作,而不是仅仅得到 hello i ger +hello+
    猜你喜欢
    • 1970-01-01
    • 2017-03-07
    • 2013-02-06
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    相关资源
    最近更新 更多