PHP中两个容器之间的CURL请求?

vcirk6k6  于 2022-11-13  发布在  PHP
关注(0)|答案(1)|浏览(146)

我有一个代理容器,它向一个API容器发出curl请求。下面是代理容器的curl请求:

$postRequest = [
        'email' => $_POST['email'],
        'password' => $_POST['password']
        ];
    pretty_var_dump($postRequest);
        
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, 'http://api:80');
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_request);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $post_request);
        $curl_response = curl_exec($curl);
        curl_close($curl);
        
        $response = json_decode($curl_response, true);

        pretty_var_dump($response);

下面是api文件的代码:

<?php

// return api results
header('Content-Type: application/json; charset=utf-8');

include_once('functions.php');
include_once('config.php');

$conn = new mysqli($host, $user, $pass, $mydatabase);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} else {
    echo "Connected to MySQL server successfully!";
}

$email = $_POST['email'];
$signin_password = $_POST['password'];



$select_query = "SELECT user_password, user_id, user_email, user_status FROM user_login WHERE user_email = '{$email}'";

echo $select_query;
$result = $conn->query($select_query);

if (isset($result)) {
    $result = $result->fetch_all(MYSQLI_ASSOC);
    pretty_var_dump($result);
    if (isset($result[0])) {
        $result = $result[0];

        if (isset($result['user_password']) && $result['user_password']) {
            if ($result['user_password']) {
                if (isset($result['user_status']) && $result['user_status'] === "1" && isset($result['user_id'])) {
                    $_SESSION['loggedIn'] = $result['user_status'];
                    response(['user_id' => $result['user_id'], 'session' => $result['user_status']], 200, 'Sign in successful');
                } elseif (isset($result['user_status']) && $result['user_status'] === "2" && isset($result['user_id'])) {
                    $_SESSION['loggedIn'] = $result['user_status'];
                    response(['user_id' => $result['user_id'], 'session' => $result['user_status']], 200, 'Sign in successful');
                }
            } else {
                $_SESSION['loggedIn'] = false;
                response('N/A', 401, 'Password incorrect');
            }
        }
    } else {
        response('N/A', 401, 'Email address not found');
    }
}

由于某种原因,API文件没有从curl中阅读POST数据,因此每次都返回null。MYSQLI,curl都已安装,如果手动向API文件传递正确的用户名和密码,它将完美工作。
任何帮助都将不胜感激!

EDIT我已经更新了curl请求的代码,但是我仍然收到空值。api代码保持不变。是否有什么明显的地方我遗漏了?

$email = $_POST['email'];
        $password = $_POST['password'];

        // $data = http_build_query($postRequest);
        $data = '{"email":"'.$email.'", "password":"'.$password.'"}';

        $headers = array(
            "Content-Type: application/json",
            "Accept: application/json",
         );
        

        pretty_var_dump($data);

        
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, 'http://127.0.0.1:86');
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        $curl_response = curl_exec($curl);
        curl_close($curl);
        
        $response = json_decode($curl_response, true);

        pretty_var_dump($response);
7cjasjjr

7cjasjjr1#

更新

我怀疑你是否需要这些头。接受:是可以的,但这不是浏览器使用的。

$headers = array(
        "Content-Type: application/json",
        "Accept: application/json",
     );

内容类型错误。内容类型包含在响应标头中,这是请求标头。
它需要是一个键值,你只需要一个字符串。$headers = array('Accept:' =〉'应用程序/json ');
在电子邮件标题中,这些需要修复。

$data = "email=$email&password=$password";

在响应中,您可以添加

header("Content-Type: application/json");

更新结束

我刚把这个贴在另一个问题上。看起来你也可以用它。
这里肯定有问题。我怀疑$post_request是否同时属于CURLOPT_POSTFIELDS和CURLOPT_HTTPHEADER。

curl_setopt($curl, CURLOPT_POSTFIELDS, $post_request);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_HTTPHEADER, $post_request)

两种方法来制作你的帖子数据。

$post = 'key1=value1&key2=value2&key3=value3';
$post = array('key1'=>value1,'key2'=>value2,'key3'=>'value3');

根据数据的不同,您可能需要使用urlencode()

$post = urlencode($post);

我做了很多 curl 这些是我的标准后选项

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_ENCODING,"");

这些是我的故障排除选项

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);

您需要查看请求(输出)和响应头(输入),因此您需要这两个选项。
这两个选项必须在解决问题后才能出来。

curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);

如果是HTTPS,则需要以下选项:

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);

我总是制作我自己的请求头。你可以删除这个选项,它不是强制性的。

$request = array();
$request[] = "Host: www.example.com";
$request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";

相关问题