运行url并从结果url-PHP

时间:2019-06-24 05:13:14

标签: php

在PHP中,我创建了一个类似于以下内容的网址:

https://example.com/as//authorization.oauth2?pfidpadapterid=ctct&client_id=1234567890&redirect_uri=https://localhost&response_type=code&scope=contact_data&

加载该网址时,我在生成的网址中得到了code,例如:

https://localhost/?code=abcd-1111-0000-ffgh

参数code:abcd-1111-0000-ffgh是我需要的结果。

所以我有请求url,必须从响应url检索代码。如何使用PHP执行此操作?

我可以为此使用CURL吗?我尝试过,但是curl会返回请求网址本身。

下面是我的代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $authURL); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_NOBODY, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_exec($ch);
$new_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 
curl_close($ch);
return $new_url;

所以我的目的是创建一个URL(已经完成),运行该URL并使用PHP以编程方式从结果url获取参数。

2 个答案:

答案 0 :(得分:0)

尝试一下:

例如,您可以使用GET获取URL参数

www.example.com/user?id=1111&Fname=john&Lname=Doe

...在您的PHP中:...

$id=$_GET['id'];
$Fname=$_GET['Fname'];
$Lname=$_GET['Lname'];

答案 1 :(得分:0)

我不确定您在寻找什么。这可能对您有帮助

$context = [
    'http' => [
        'method' => 'GET',
        'max_redirects' => 5,
    ],
];

@file_get_contents('https://example.com/...//', null, stream_context_create($context));
echo '<pre>';print_r($http_response_header);die();

$ http_response_header将具有位置数据