繁体   English   中英

收集和使用从http Curl(php)帖子返回的数据

[英]Collecting and using data returned from a http Curl (php) Post

我需要帮助使用http帖子后返回的数据。 以下是我目前的帖子代码,用于将数据发布到特定网站

<?php
//extract data from the post
extract($_POST);

//set POST variables
$url = 'https://test.com';
$fields = array(
                        'surname' => urlencode($surname),
                        'first_name' => urlencode($first_name),
                        'dob' => urlencode($dob),
                        'email' => urlencode($email),
                        'home_phone' => urlencode($home_phone),
                        'mobile_phone' => urlencode($mobile_phone),
                        'work_phone' => urlencode($work_phone),
                        'postcode' => urlencode($postcode),
                        'leadid' => urlencode(123),
                        'affid' => urlencode(123),
                        'subid' => urlencode(123),
                        'bank_acno' => urlencode($bank_acno),
                        'bank_sort' => urlencode($bank_sort),
                        'amount_required' => urlencode($amount_required),

                );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

?>

我得到2个响应之一 - {result:true}或{result:false}

我需要知道如何执行以下操作:

当我得到{result:true}时我想重定向到网址“ http://thisurl.com ”当我得到{result:false}时我想返回一个回复“拒绝”

请注意,响应不是json,它只是以该格式返回。

您需要检查$result变量包含的内容,然后根据情况进行重定向或显示被拒绝的消息:

<?php
//extract data from the post
extract($_POST);

//set POST variables
$url = 'https://test.com';
$fields = array(
                        'surname' => urlencode($surname),
                        'first_name' => urlencode($first_name),
                        'dob' => urlencode($dob),
                        'email' => urlencode($email),
                        'home_phone' => urlencode($home_phone),
                        'mobile_phone' => urlencode($mobile_phone),
                        'work_phone' => urlencode($work_phone),
                        'postcode' => urlencode($postcode),
                        'leadid' => urlencode(123),
                        'affid' => urlencode(123),
                        'subid' => urlencode(123),
                        'bank_acno' => urlencode($bank_acno),
                        'bank_sort' => urlencode($bank_sort),
                        'amount_required' => urlencode($amount_required),

                );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

$result_true   = 'true';
$check_true = strpos($result, $result_true);

if ($check_true !== false) {
     header('Location: http://url.com');
     die;
}

$result_false   = 'false';
$check_false = strpos($result, $result_false);

if ($check_false !== false) {
     echo "Denied";
}

在您使用的代码中:

curl_setopt($ch,CURLOPT_POST, count($fields));

但在php.net手册中:

CURLOPT_POST:TRUE执行常规HTTP POST。 此POST是普通的application / x-www-form-urlencoded类型,最常用于HTML表单。

所以计算字段不是正确的方法,它可能是真或假不计数。 当然,如果$fields数组为空,结果将为0 ,等于FALSE或者如果它为1,则它为1TRUE ),但是当计数结果大于1 ,这是没有意义的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM