从CURL响应返回URL参数

时间:2015-04-27 05:31:13

标签: php curl

如何从CURL响应中获取URL参数。

发送数据时,我使用POST方法example.com

但他们发回的回应是以下结构。 但我怎么能这样做?

example.com/?newParamKey1=value1&newParamKey2=value2

我想阅读 newParamKey1 &的 newParamKey2

POST数据的CURL代码

$postData['return_url'] = "http://return_data_in_this_url.com";

    curl_setopt ($ch, CURLOPT_URL, "http://example.com"); 
    curl_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $status = curl_getinfo($ch);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 2);
    $return_data = curl_exec($ch);

第三方发送回以下结构的回复。

http://return_data_in_this_url.com/?newParamKey1=value1&newParamKey2=value2

curl_getinfo($ch)

Array ( [url] => https://....../thirdpartyurl.php 
        [content_type] => [http_code] => 0 
        [header_size] => 0 
        [request_size] => 0 
        [filetime] => 0 
        [ssl_verify_result] => 0 
        [redirect_count] => 0 
        [total_time] => 0 
        [namelookup_time] => 0 
        [connect_time] => 0 
        [pretransfer_time] => 0 
        [size_upload] => 0 
        [size_download] => 0 
        [speed_download] => 0 
        [speed_upload] => 0 
        [download_content_length] => -1 
        [upload_content_length] => -1 
        [starttransfer_time] => 0 
        [redirect_time] => 0 
        [redirect_url] => [primary_ip] => [certinfo] => Array ( )            
        [primary_port] => 0 
        [local_ip] => [local_port] => 0 
      )

2 个答案:

答案 0 :(得分:0)

在PHP中,您可以通过以下方式读取PHP代码中的查询字符串参数:

<?php

  if(isset($_GET["newParamKey1"]){
     $var1 = $_GET["newParamKey1"];
   }
  if(isset($_GET["newParamKey2"]){
     $var2 = $_GET["newParamKey2"];
   }    
?>

答案 1 :(得分:0)

由于网站返回了一个带有POST / GET in key =&gt;值对的网址,您只需将对象存储到变量中,如下所示

$postObject=$_REQUEST;

然后运行foreach循环,如下所示

foreach($postObject as $key=>$val) { echo $key.' = '.$val; }

跳这将解决您的问题