卷曲不将值传递给php文件

时间:2014-03-29 18:49:02

标签: php html post curl

我有一个名为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 ;
    ?>

2 个答案:

答案 0 :(得分:1)

将帖子字段设置为如下数组:

<?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);
?>

答案 1 :(得分: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);
?>