通过curl在php中发送数据的问题

时间:2018-08-05 10:52:46

标签: php curl

我们已经用php编写了curl脚本。

$urlFinal="timestamp=".$timeStamp. "&vender_id=8&industry_id=".$companyName."&stationId=".$stationName."&analyserId=".$deviceIdOrSlNoNew."&processValue=".$dataValue."&scaledValue=".$dataValue."&flag=U".htmlspecialchars("&")."parameter=".$paramterName."&unit=".$unitKey;

$url="http://www.example.com/store/WebService.asmx/getdata?".$urlFinal;


echo "<br /><br /><br />";
echo $url;
echo "<br /><br /><br /><br />";

$urlNew=str_replace("&amp;","&",$url);  

echo "<br />".$urlNew;

echo "<br /><br /><br /><br />";



$ch = curl_init($urlNew);

curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
echo $data;

echo "<br />".$data;

我们在浏览器中得到以下输出

http://www.example.com/store/WebService.asmx/getdata?timestamp=2018-08-04:12:00:00.AM&vender_id=18&industry_id=ABC有限公司,化学事业部&stationId = Abcl Na-hypo Stack&analyserId = ARSN:1853&processValue = 4.70&scaledValue = 4.70&flag = U&parameter = Cl2&unit = ug / m3

http://www.example.com/store/WebService.asmx/getdata?timestamp=2018-08-04:12:00:00.AM&vender_id=18&industry_id=ABC工业有限公司,化学事业部&stationId = Abcl Na-hypo Stack&analyserId = ARSN:1853&processValue = 4.70&scaledValue = 4.70&flag =U¶meter= Cl2&unit = ug / m3

错误请求 HTTP错误400。请求的格式错误。

错误请求 HTTP错误400。请求的格式错误。

如何解决Curl中的&para问题。当我们粘贴以下浏览器地址栏时,上面的方法非常适合

http://www.example.com/store/WebService.asmx/getdata?timestamp=2018-08-04:12:00:00.AM&vender_id=18&industry_id=ABC有限公司,化学事业部&stationId = Abcl Na-hypo Stack&analyserId = ARSN:1853&processValue = 4.70&scaledValue = 4.70&flag = U&parameter = Cl2&unit = ug / m3

2 个答案:

答案 0 :(得分:0)

您可能应该在curl_init之前使用urlencode函数。

string urlencode ( string $str )

$url = urlencode($url);

然后调用curl_init。

https://php.net/manual/en/function.urlencode.php

答案 1 :(得分:0)

您不需要urlencode或对实际的URL进行任何替换-通过GET的东西应该具有适当的“&”号(&),等等,而不是html实体或百分比转义符代码等效。

您确实需要对单个数据项进行处理-因此您的$companyName等需要进行URL编码,然后与变量名粘合在一起以构建整个GET请求

我还要问-您是否将cURL用于GET请求,因为对各种fopen类型的调用(包括file_get_contents使用http都被禁用了吗?因为对于GET请求,{{1 }}比cURL更容易...