当前位置: 首页 > 知识库问答 >
问题:

PHP curl post调用不起作用,而是重定向到登录页面

姬心思
2023-03-14

尝试调用一个java服务从PHP客户端使用curl函数。我的api期待一个json请求体,如下所示,

 {"personRequest":{"emailId":"rupanjan@gmail.com","isActive":"true"}}

我的php客户端代码如下所示,

    $postData['personRequest'] = array(
        'emailId' => $emailId,
        'isActive' => "true"
    );


    //$data = Array();
    //$data['personRequest']['isActive'] = "true";
    //$postFields = json_encode($data);

    $postFields = json_encode($postData);

    $url = "$server_url" . "service/membership/update";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($postFields))
    );

    $result = curl_exec($ch);

    var_dump(curl_getinfo($ch));
    curl_close($ch);

    echo $ch;
    print_r($result);

执行后,它显示一个空白的响应,我也可以访问服务器代码,试图把调试点在我的java代码,似乎超文本传输协议调用没有到达我的服务器。.

刚才执行var_dump(curl_getinfo($ch));得到下面的输出,数组(22){["url"]=

这意味着这是重定向到登录页面,有人能帮我吗...我不知道为什么会这样...

共有2个答案

濮彬
2023-03-14
in LOGIN page
================

$url = $server_url . "login.html";
        $postdata = http_build_query(array(
            'username' => $username,
            'password' => $password
                ));

        $tmp_fname = tempnam("/tmp", "COOKIE");
        $userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $tmp_fname);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/x-www-form-urlencoded',
            'Accept: application/json')
        );
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        $result = curl_exec($ch);
        curl_close($ch);


in SUBSEQUENT requests
===========================

I have to add the cookies for every protected resource call...

$id = $this->input->post('id');
        $emailId = $this->input->post('emailId');
        $password = $this->input->post('password');
        $tmp_fname = $this->session->userdata('cookies_file');
        $userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";

        $personObj['personRequest'] = array(
            'emailId' => $emailId,
            'isActive' => "true"
        );

        $postdata = json_encode($personObj);


        $url = $server_url . 'service/membership/update';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $tmp_fname);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Accept: application/json')
        );
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
        $result = curl_exec($ch);
        curl_close($ch);
充栋
2023-03-14

您应该使用curl\u setopt($ch,CURLOPT\u POST,1) 而不是curl_setopt($ch,CURLOPT_CUSTOMREQUEST,“POST”)

<?php
    $postData['personRequest'] = array(
        'emailId'=>"rupanjan@gmail.com",
        'isActive' => "true"
    );

    $postFields = json_encode($postData['personRequest']); /JSON encoding the array

    $url = "$server_url" . "service/membership/update";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$postFields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //Removed the header... 
    $result = curl_exec($ch);
    curl_close($ch);

    echo $result;

 类似资料:
  • 记录器文件中的日志- org.springframework.Security.Access.event.loggerlistener-安全授权失败,原因是:org.springframework.Security.Access.accessdeniedexception:访问被拒绝;通过身份验证的主体:org.springframework.security.authentication.ano

  • 我现在一直在努力使用我的登录页面来让组件呈现Loggedin组件。我的前端是Reactjs,后端是NodeJS。我对nodejs、expression和react都是新手。 在loginform组件上,我使用fetch进行了一次post,它将用户名和密码传递给后端的相应endpoint。没问题。在后端,它读取我存储用户(不使用任何数据库)的jsonfile来查找匹配项,如果用户名和密码都匹配,则它

  • 在使用SAML for ADFs登录Spring security度过了一段美好的时光后,我成了框架的粉丝。我能够将我的web应用程序与ADFS集成以进行登录。但我缺少几个要求:1)当我们在我的web应用程序登录页面上选择IDP ADFS登录url时,它将从我的web应用程序转移到ADFS登录。这不是商业上的欲望行为。2) 同样面临的问题是,在成功登录时,用户对象从ADFS发送回我的web应用程序

  • 现在,当我试图访问以下URL:时,它会将我重定向到页面。 如何进行配置以返回而不是重定向到登录页?

  • 无论我从哪里登录,用户都再次被引导到主页“/”,而不是登录前请求的页面。 例如,不受保护,任何人都可以查看它。但是,如果用户试图从此页面登录,他们将被重定向到主页,而不是在登录后停留在。如何配置安全性以防止这种情况发生? 即使从URL登录,用户也会被重定向到主页。 但是在提交登录表单之后,引用器将从预期的url更改为。因此,当我实现一个SuccessShandler重定向到referer url时

  • 问题内容: 我想设置我的网站,以便如果用户点击该页面并且他们已经登录,它将把他们重定向到主页。如果未登录,它将正常显示。由于登录代码内置在Django中,我该怎么做? 问题答案: 我假设你当前正在使用内置的登录视图, 或你网址中的类似内容。 你可以编写包含默认视图的登录视图。它将检查用户是否已经登录,如果已经登录则重定向,否则使用默认视图。 就像是: 当然可以相应地更改你的网址: