https请求到使用php的重定向的另一个域 [英] https request to another domain with redirect using php

查看:187
本文介绍了https请求到使用php的重定向的另一个域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个网域上有自动登录链接 - 此网域用于授权:



https://example.com/api / login / {key} / {username}



点击此链接后,用户将被重定向到控制面板:



https://example.com



重定向到



https://example.com/login页面。



在另一个域 example2.com 用户有结算面板和链接 https://example.com/api/login/ {



因此,我需要从 example2.com 进行检查

/ code>到 example.com 如下:

  $ url = https://example.com/api/login/{key}/{username}; 
if function-to-check-autologin($ url)==true{
// do something
}

我的搜索结果是curl,但我不能自己配置请求。



我该怎么办? curl或其他方式?

解决方案

虽然你的问题很模糊,但我会给你一些线索。 b

首先,你必须将 CURLOPT_SSL_VERIFYPEER 选项设置为 false 要处理同行的证书。



此外,你必须告诉CURL处理重定向。这假设您设置了 CURLOPT_FOLLOWLOCATION 选项以及 CURLOPT_MAXREDIRS CURLOPT_AUTOREFERER



为了发送带有请求的POST数据,您必须将 CURLOPT_POST 选项设置为 true CURLOPT_POSTFIELDS 到POST数据或经过url编码的字符串。



一旦CURL请求完成,您就可以通过 curl_getinfo 函数。使用 CURLINFO_REDIRECT_COUNT 作为第二个参数调用它会给出重定向的数量, CURLINFO_EFFECTIVE_URL - 最后有效的URL。



因此代码可能如下所示:

  $ ch = curl_init(); 
curl_setopt_array($ ch,array(
CURLOPT_URL => $ url,
CURLOPT_NOBODY => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 1,
CURLOPT_AUTOREFERER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $ data_array,
));
$ response = curl_exec($ ch);
if(curl_getinfo($ ch,CURLINFO_REDIRECT_COUNT)== 1&& curl_getinfo($ ch,CURLINFO_EFFECTIVE_URL)== $ sample_redirect_url)
echo'OK';


I have auto-login link on one domain - this domain is used for authorization:

https://example.com/api/login/{key}/{username}

After following this link, user is redirected into control panel:

https://example.com

If something is wrong - user redirected to

https://example.com/login page.

In another domain example2.com users have billing panel and link https://example.com/api/login/{key}/{username} is placed in it.

So i need to do check from example2.com to example.com like this:

$url = https://example.com/api/login/{key}/{username};
if function-to-check-autologin($url) == "true" {
  //do something
}

My searching result is curl, but i cant configurate request by myself.

How could I do it? curl or another way?

解决方案

Although your question is vague, I would give you some clues.

First of all, you have to set CURLOPT_SSL_VERIFYPEER option to false unless you're going to deal with peer's certificate.

Also you have to tell CURL to handle redirects. This assume that you set CURLOPT_FOLLOWLOCATION option as well as CURLOPT_MAXREDIRS and CURLOPT_AUTOREFERER.

In order to send POST data with request you have to set CURLOPT_POST option to true and CURLOPT_POSTFIELDS to array of POST data or url-encoded string.

Once CURL request is done you can learn all information about it by curl_getinfo function. Calling it with CURLINFO_REDIRECT_COUNT as 2nd parameter will give number of redirects, and CURLINFO_EFFECTIVE_URL - last effective URL.

So the code might look like this:

$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => $url,
    CURLOPT_NOBODY => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_MAXREDIRS => 1,
    CURLOPT_AUTOREFERER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $data_array,
));
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_REDIRECT_COUNT) == 1 && curl_getinfo($ch, CURLINFO_EFFECTIVE_URL) == $sample_redirect_url)
    echo 'OK';

这篇关于https请求到使用php的重定向的另一个域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆