Php CURL Post,不发送数据

时间:2013-10-24 19:27:02

标签: php post curl

我可以使用Ajax POST到我在我的网站上的php文件但是当我尝试使用php发布到相同的php文件时,我的服务器似乎没有获得任何数据而且它不会发布 这是我在服务器日志中获得的消息: “POST /login_exec.php HTTP / 1.0”302 447“ - ”“Mozilla / 4.0(兼容; MSIE 6.0; Windows NT 5.1)” 我尝试了很多不同的卷曲脚本,但我似乎无法找到为什么它不起作用。 这是我目前使用的代码 - (我尝试了很多不同的选项)

     <?php

//create array of data to be posted
$post_data['username'] = 'yarden';
$post_data['password'] = 'yarden';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//create cURL connection
$curl_connection = curl_init('http://mydomain.org.il/login_exec.php');

//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' . curl_error($curl_connection);

//close the connection
curl_close($curl_connection);

?>
抱歉我的英语,请帮帮我:) 感谢

1 个答案:

答案 0 :(得分:0)

似乎302是重定向状态代码。

CURLOPT_FOLLOWLOCATION => true 添加到您的数组

$curlConfig = array(
        CURLOPT_URL            => "http://mydomain.org.il/login_exec.php",
        CURLOPT_POST           => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_POSTFIELDS     => array(
            'username' => 'yarden',
            'password' => 'yarden',
        ),
    );