【问题标题】:display variables received with http-post显示通过 http-post 接收到的变量
【发布时间】:2013-12-02 08:59:32
【问题描述】:

我使用“curl”将一些变量从我的网站发送到远程 URL:

$url = "a remote url";
$fields =array( variables );

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

$output = curl_exec ($ch);


if (!curl_exec($ch)) {
    echo 'An error has occurred: ' . curl_error($ch);
}
else {
    echo 'everything was successful';
}
curl_close ($ch);
echo $output;

我得到“一切都成功”,但我不知道如何显示从远程 URL 接收到的变量。

谁能告诉我如何显示“$_POST”中的变量? 我想检查变量是否正确发送到远程 URL。

【问题讨论】:

    标签: php curl http-post


    【解决方案1】:

    简单使用:

    print_r( $_POST );
    

    或者在“发送”脚本中将字段显示为数组:

    print_r( $fields );
    

    假设您的 CURL 响应是纯文本格式:

    variable1=value2
    variable2=value3
    

    您仍然可以通过这种方式查看您的回复:

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_COOKIESESSION, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    
    $output = curl_exec ($ch);
    
    
    if (!curl_exec($ch)) {
        echo 'An error has occurred: ' . curl_error($ch);
    }
    else {
    
        $rows = explode("\n", $output);
    
        for( $i=0;$i<count($rows);$i++ )
        {
          $cols = explode('=', trim($rows[$i]) );
          print_r($cols);
        }
    }
    
    curl_close ($ch);
    echo $output;
    

    【讨论】:

    • 我真正需要的是如何从第二个网站恢复变量,并用它们做一些其他的事情
    • 哦,这就是重点。因此,您需要从 CURL 调用中获取有效的数据格式。现在您将所有内容都存储在变量 $output 中。您使用的是什么格式的数据交换? XML/JSON/纯文本?向我们展示 print_r( $output );和 print_r( var_dump($output) );
    • 很抱歉,我在解释我的真正需求时可能遇到了一些麻烦。事实上,从我的网站上,我得到了正确的响应。我需要的是从另一个网站(接收发布请求的网站)连接并显示变量
    • 在 CURL 中,您正在发出请求。如果请求是基于 POST 的,则需要通过 $fields = array('variable1' => 'value1', 'var2' => 'val2'); 传递 post-data并设置 CURLOPT_POSTFIELDS。 (这你已经成功完成了。)然后 CURL_EXEC 为你提供来自远程 $url 的响应数据。如果您需要处理此响应(数据),则需要对其进行解析。没有其他选择。如果您可以向我显示转储的响应数据,我可以为您提供进一步的支持。
    猜你喜欢
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    • 2013-03-12
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    相关资源
    最近更新 更多