简体   繁体   English

php cURL-POST无法正常工作

[英]php cURL - POST Not working

Trying some simple code to work with cURL but no lucky.. I tried to POST a form but nothing happens and no error occurs. 尝试一些简单的代码来使用cURL,但是不走运。.我尝试过POST表单,但是没有任何反应,也没有发生错误。

Here is my code: 这是我的代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8888/curl/home2.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'usuario=teste&senha=12345');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);

print_r(curl_getinfo($ch));
echo curl_errno($ch) . '-' . 
                curl_error($ch);

echo $store;

When submit happens on the form home2.php it should go to another page and return some data. 当提交形式为home2.php时,它应该转到另一个页面并返回一些数据。 Am i doing something wrong? 难道我做错了什么?

--- UPDATE -更新

Here is my simple home2.php code: 这是我简单的home2.php代码:

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <form method="POST" action="login.php" id="formLogin" name="formLogin">
        Usuario <input name="login" type="text">
        Senha <input name="senha" type="text">
        <input type="submit">
    </form>
</body>
</html>

login.php : login.php:

<?php

$login = $_POST['login'];
$senha = $_POST['senha'];

if($login == "teste"){
    echo "OK";
}

?>

It should be "OK" as my result, right? 结果应该是“ OK”,对吗?

Your form on home2.php looks like this: 您在home2.php上的home2.php如下所示:

<form method="POST" action="login.php" id="formLogin" name="formLogin">
    Usuario <input name="login" type="text">
    Senha <input name="senha" type="text">
    <input type="submit">
</form>

It's important to note that the username input box is named login , yet when you create the cURL request, you're sending the username as usario . 请务必注意,用户名输入框名为login ,但是在创建cURL请求时,您会将用户名作为usario发送。 This needs to be changed. 这需要更改。 You also need to emulate what a browser would do and send the post variables towards login.php ( action="login.php" ) instead of home2.php (this page only includes the form, and never receives any of the input form data). 您还需要模拟浏览器将执行的操作,并将post变量发送到login.phpaction="login.php" )而不是home2.php (此页面包含表单,并且从不接收任何输入表单数据) )。

All in all, your code should be fixable by: 总而言之,您的代码应可通过以下方式修复:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8888/curl/login.php');
// Change the URL                                         ^^^^^
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'login=teste&senha=12345');
// Change the username key             ^^^^^
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);

print_r(curl_getinfo($ch));
echo curl_errno($ch) . '-' . 
                curl_error($ch);

echo $store;

If you're ever in doubt, the network tab in your browser (or any HTTP packet sniffer) will tell you exactly what requests the browser makes, with what variables, towards what URLs, and you'll be able to replicate it way easier. 如果您有疑问,浏览器中的“网络”选项卡(或任何HTTP数据包嗅探器)将告诉您浏览器发出的确切请求,带有哪些变量,指向哪些URL的地址,并且您可以更轻松地复制它。

Note : If your success.php page that login.php apparently redirects to on success does anything at all, you'll also need to have this be called automatically. 注意 :如果您在success.php重定向到login.phpsuccess.php页面根本没有执行任何操作,则还需要自动调用此页面。

If login.php automatically redirects, you can just use the cURL option CURLOPT_FOLLOWLOCATION set to 1. 如果login.php自动重定向,则只需使用将cURL选项CURLOPT_FOLLOWLOCATION设置为1。

You mentioned "When submit happens on the form home2.php it should go to another page". 您提到“当提交发生在home2.php表单上时,它应该转到另一页”。 By that do you mean that there is an HTTP redirect? 就是说,这意味着存在HTTP重定向? If so, you need to tell cURL to follow redirects. 如果是这样,您需要告诉cURL遵循重定向。 Like this: 像这样:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM