$ _POST通过cURL无法正常工作

时间:2015-03-16 07:35:37

标签: php api post curl

我正在尝试使用curl发布一些数据,我没有收到我的帖子内容而无法找到问题的根源

curl.php

<?php 

$data = array("user_email" => "22" , "pass" => "22" ); 
$string = http_build_query($data);


$ch = curl_init("http://localhost:8888/290_project/test.php"); //this is where post data goes too, also starts curl
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_exec($ch);
curl_close($ch) //ends curl
?>

test.php的

<?php 
if(isset($_POST['user_email'], $_POST['pass'])) {

$name = $_POST['user_email'];
$pass = $_POST['pass'];

echo $name;
echo $pass;

} else {
echo "error";
} ?>

每次我收到错误回复都意味着帖子数据没有通过。我已经尝试了一切我能想到的麻烦射击;我一定是在寻找一些我根本不熟悉的东西?

3 个答案:

答案 0 :(得分:2)

请将CURLOPT_URL设为http://localhost:8888 .....

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8888/290_project/test.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);

if(!curl_errno($ch)){ 
$info = curl_getinfo($ch); 
} else { 
echo  'Curl error: ' . curl_error($ch); 
} 
curl_close($ch) //ends curl
?>

使用curl_getinfo() - 获取有关上次传输的信息。 有关详细信息,请参阅以下链接: - http://php.net/manual/en/function.curl-getinfo.php

我已经编辑了答案,错误的原因不是卷曲。

http_build_query($data, '', '&');

答案 1 :(得分:0)

以下是工作示例。请试试这个。

$postData = array(
        'user_name' => 'abcd',
        'password' => 'asdfghj',
        'redirect' => 'yes',
        'user_login' => '1'
    );
$url='http://localhost:8888/290_project/test.php';
$ch = curl_init();
//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $url);
// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
//Set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute the request
$store = curl_exec($ch);
print_r($store);
curl_close($ch)

答案 2 :(得分:-1)

您的PHP代码中有错误:

编辑:

if(isset($_POST['user_email'], $_POST['pass'])) {

为:

if(isset($_POST['user_email']) && isset($_POST['pass'])) {