2015-04-15 38 views
3

我需要使用http post後返回的數據的幫助。 下面是我目前的職位代碼,並將數據到特定的網站收集和使用從http返回的數據Curl(php)Post

<?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響應的任何一個 - {結果:TRUE}或{結果:假}

我需要知道的是如何做到以下幾點:

當我拿到{結果:真正}我想重定向到URL「http://thisurl.com」 當我拿到{結果:假}我要回一個響應「拒絕」

請注意th e響應不是json,它只是以這種格式返回。

+0

是返回的數據真正的json?順便說一下,你可能希望看看PHP文檔中的http_build_query用於構建你的發佈數據 – RamRaider

回答

1

您需要檢查什麼$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是正常的 應用程序/ x-www-form-urlencoded類型,最常用的格式爲HTML 。

所以計數字段不是正確的方法來做到這一點,它可能是TRUE或FALSE而不是計數。當然,如果$fields數組爲空,結果將是0,它等於FALSE,或者如果它是1,那麼它是1TRUE),但是當計數的結果大於1時,這是沒有意義的。

+0

謝謝你的座右銘,你能否給我提供一個完整的解決方案。將你的解決方案添加到我的代碼中,因爲我知道把它放在哪裏 – MatHatrik

+0

@MaxPayneUK在你的代碼的最後一段之後添加這個。在curl_close和結束標記之間'?>' – motto

+0

@MaxPayneUK我已經更新了我的答案。讓我知道它對你來說是否正確。 – motto