如何使用PHP在url中发布值

时间:2016-09-21 05:58:31

标签: php web-services

我正在创建一个Web服务。我试图写一个URL但它的抛出错误。我没有得到我错的地方。我想在url中传递这些变量值,并根据这个我想调用Web服务

<?php
if($_POST["occupation"] == '1'){
    $occupation = 'Salaried';
}
else{
    $occupation = 'Self+Employed';
}

$url = 'http://www.aaa.com/ajaxv2/getCompareResults.html?interestRateType='.$_POST["interestRateType"]'.&occupation='.$_POST["occupation"].'&offeringTypeId='.$_POST["offeringID"].'&city='.$_POST["city"].'&loanAmt='.$_POST["loanAmt"].'&age='.$_POST["age"];
echo $url;
//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
$json = json_decode($result, true);

//print_r($json); 
//echo $json['resultList']['interestRateMin'];
$json_array = $json['resultList'];

print_r($json_array);

?>

2 个答案:

答案 0 :(得分:1)

尝试以下代码。您在&occupation

之前有语法错误
$url = 'http://www.aaa.com/ajaxv2/getCompareResults.html?interestRateType='.$_POST["interestRateType"].'&occupation='.$_POST["occupation"].'&offeringTypeId='.$_POST["offeringID"].'&city='.$_POST["city"].'&loanAmt='.$_POST["loanAmt"].'&age='.$_POST["age"];

答案 1 :(得分:0)

复制此内容,因为有一些'.错误

$url = 'http://www.aaa.com/ajaxv2/getCompareResults.html?
        interestRateType='.$_POST["interestRateType"].'&
        occupation='.$_POST["occupation"].'&
        offeringTypeId='.$_POST["offeringID"].'&
        city='.$_POST["city"].'&
        loanAmt='.$_POST["loanAmt"].'&
        age='.$_POST["age"];