【问题标题】:Parsing value from search field to http request将搜索字段中的值解析为 http 请求
【发布时间】:2015-04-13 15:57:01
【问题描述】:

有两个页面。

http://miveo.dk/searchform.php http://miveo.dk/search.php?go

我正在尝试将搜索表单中的输入值解析为在 php 和 curl 中完成的 http 请求。

查看上面写着的行:看这里。

我确实回显了 $name 值并正确解析了该值。但它不会被解析为我尝试执行的 HTTP 请求。

任何想法我做错了什么? 搜索.php:

<html>
<body>
<?php 
if(isset($_POST['submit'])){ 
if(isset($_GET['go'])){ 
if(preg_match("/[A-Z  | a-z]+/", $_POST['name'])){ 
$name = $_POST['name'];       <-- LOOK HERE
$url  = 'http://dev.api.ean.com/ean-services/rs/hotel/v3/list?minorRev=28';
$url .= '&apiKey=nwdzm6ph8j8qss4yg8w7pyfy';
$url .= '&cid=55505';
$url .= '&destinationString=$name'; <---- LOOK HERE
$url .= '&searchRadius=50';
$url .= '&supplierCacheTolerance=MED_ENHANCED';
$url .='&arrivalDate=09/04/2015&departureDate=09/05/2015&room1=2';
$header[] = "Accept: application/json";
$header[] = "Accept-Encoding: gzip";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$response = json_decode(curl_exec($ch));

} 
  else{ 
  echo  "<p>Please enter a search query</p>"; 
  }
}
} 
?> 
<table>
<Tr>
<TD>
<?PHP
echo $name;
//for ($x=0; $x <=9; $x++)
//{
//print_r($url);
print_r("<BR>");
print_r("<pre>");
print_r ($response);
print_r("</pre>");
//}
?>
</TD>
</Tr>
</table>
</body>
</html>

【问题讨论】:

    标签: php html rest


    【解决方案1】:

    您需要:

    $url .= "&destinationString=$name"; <---- LOOK HERE
    

    $url .= '&destinationString=' . $name; <---- LOOK HERE
    

    PHP 只在双引号字符串中进行变量替换,而不是在单引号字符串中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-12
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 2012-10-26
      • 1970-01-01
      相关资源
      最近更新 更多