Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php cURL重定向返回的URL_Php_Json_Curl - Fatal编程技术网

Php cURL重定向返回的URL

Php cURL重定向返回的URL,php,json,curl,Php,Json,Curl,我想将数据从站点A发送到站点B。我已成功传输数据,但我想将url重定向到从Curl生成的返回url。下面是我的代码 $jsonData = array( 'first_name' => "$fname", 'last_name' => "$lname", 'phone_number' => "$phone", 'gender' => "$gender", 'email' => "$email", 'businessname' => "$businessname

我想将数据从站点A发送到站点B。我已成功传输数据,但我想将url重定向到从Curl生成的返回url。下面是我的代码

$jsonData = array(
'first_name' => "$fname",
'last_name' => "$lname",
'phone_number' => "$phone",
'gender' => "$gender",
'email' => "$email",
'businessname' => "$businessname",
'natureofbusiness' => "$biznature", 
'address' => "$address", 
'utm' => "esg", 
'date' => "$d"
);

$jsonDataEncoded = json_encode($jsonData);

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

$result = curl_exec($ch);
这就是结果

{"data":{"success":true,"redirectUrl":"https://url.com/authorize
/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODU5MDg4MzgsImRhdGEiOnsidXRtIjoiZXNnIiwiZW1haWwiOiJjbm5lYnVlNGFsbEB5YWhvby5jb20iLCJmaXJzdE5hbWUiOiJDaGlt
YSIsImxhc3ROYW1lIjoiT3NjYXIiLCJjb250YWN0TnVtYmVyIjoiOTg3MzczNjM3MyIsInRpbWVTdGFtcCI6IjE1ODU5MDc4NTUifSwiaWF0IjoxNTg1OTA4NTM4fQ.v6ecH7Tu5WB0ZkK-
U2ob_sQRSNn13rOU95Zo4BgwSF4?utm=esg","status":200}}

我需要帮助重定向到该Url。

将JSON结果转换为数组

$result = '{"data":{"success":true,"redirectUrl":"https://url.com/authorize/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODU5MDg4MzgsImRhdGEiOnsidXRtIjoiZXNnIiwiZW1haWwiOiJjbm5lYnVlNGFsbEB5YWhvby5jb20iLCJmaXJzdE5hbWUiOiJDaGltYSIsImxhc3ROYW1lIjoiT3NjYXIiLCJjb250YWN0TnVtYmVyIjoiOTg3MzczNjM3MyIsInRpbWVTdGFtcCI6IjE1ODU5MDc4NTUifSwiaWF0IjoxNTg1OTA4NTM4fQ.v6ecH7Tu5WB0ZkK-U2ob_sQRSNn13rOU95Zo4BgwSF4?utm=esg","status":200}}';
$result=json_decode($result,true)

现在从数组中获取url

if(isset($result["data"])){
        if($result["data"]["success"]==true){
            $url  = $result["data"]["redirectUrl"];
            header("location:".$url);
        }
    }

将JSON结果转换为数组

$result = '{"data":{"success":true,"redirectUrl":"https://url.com/authorize/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1ODU5MDg4MzgsImRhdGEiOnsidXRtIjoiZXNnIiwiZW1haWwiOiJjbm5lYnVlNGFsbEB5YWhvby5jb20iLCJmaXJzdE5hbWUiOiJDaGltYSIsImxhc3ROYW1lIjoiT3NjYXIiLCJjb250YWN0TnVtYmVyIjoiOTg3MzczNjM3MyIsInRpbWVTdGFtcCI6IjE1ODU5MDc4NTUifSwiaWF0IjoxNTg1OTA4NTM4fQ.v6ecH7Tu5WB0ZkK-U2ob_sQRSNn13rOU95Zo4BgwSF4?utm=esg","status":200}}';
$result=json_decode($result,true)

现在从数组中获取url

if(isset($result["data"])){
        if($result["data"]["success"]==true){
            $url  = $result["data"]["redirectUrl"];
            header("location:".$url);
        }
    }

谢谢你。但这并没有重定向url。我甚至试着打印解码后的$result,但什么也没打印出来。数据是作为一个对象输入的,$result->data.redirectUrl应该可以工作,但我认为我没有解析正确的语法如果你想从对象获取url,那么就使用$result->data->redirectUrl谢谢。但这并没有重定向url。我甚至试着打印解码后的$result,但什么也没打印出来。数据是作为一个对象输入的,$result->data.redirectUrl应该可以工作,但我认为我没有解析正确的语法,如果你想从对象获取url,那么就使用$result->data->redirectUrl