从其他服务器登录后重定向到服务器

时间:2016-07-15 09:05:31

标签: php curl

我有两台服务器(比如server1和server2)。 server1上有一个登录面板,如果我使用server1登录,那么我需要重定向到server2。

这是我的卷曲代码:

Sample Curl request
curl -X POST -H "user_name: aaa" -H "auth_type: email" -H "app_key: someapikey" -H "platform: Android_MDA" -H "password: 123456" -H "Cache-Control: no-cache" -H "Content-Type: application/x-www-form-urlencoded" -d '' "here is server1 login url"

如何使用PHP实现这一点。

Here is what I had done so far:
<?php

   $post_fields = '{"login": "aaa", "password": "123456"}';
   $cookie_file = tempnam('/temp', 'cookie');
   $ch = curl_init('here is server1 login url');

   curl_setopt($ch, CURLOPT_HEADER, 0);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_body);
   curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
   curl_exec($ch);

 // Execute request and read response
 $response = curl_exec($ch);

 // Check errors
 if ($response) {
         echo $response . "\n";
 } else {
         $error = curl_error($ch). '(' .curl_errno($ch). ')';
         echo $error . "\n";
 }

 // Close connection
 curl_close($ch);
 ?>

我得到的回应: 无法连接到server1(即使用的网址):连接超时(7)

1 个答案:

答案 0 :(得分:0)

  1. $ post_body未定义。
  2. 您正在使用cookie_file但是在您工作的curl情况下,没有发送cookie。所以在列表中删除与路径相关的cookie。
相关问题