将CURL结果保存在变量PHP中

时间:2017-06-06 21:34:35

标签: php curl

我是PHP的整个CURL库的新手,并设法使用以下代码从网站获取发布数据:

 <?php
    $postcode="6942GJ"; 
    $huisnummer="8";
    $marktsegment="27";
    $aansluiting="-";
    $url="http://notimportantforstackoverflow.com"; 
    $postdata = "postcode=".$postcode."&huisnummer=".$huisnummer."&marktsegment=".$marktsegment."&aansluiting=".$aansluiting; 
    $ch = curl_init(); 
    curl_setopt ($ch, CURLOPT_URL, $url); 
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    $result = curl_exec ($ch); 
    curl_close($ch);
    ?>

返回下表:

<table cellspacing="1" cellpadding="1" border="0">
		<tbody><tr valign="bottom">
			<td class="resultHeader">
				Ean code aansluiting
			</td>
			<td class="resultHeader">
				Straatnaam
			</td>
			<td class="resultHeader">
				Huisnr.
			</td>
			<td class="resultHeader">
				Huisnr. toev.
			</td>
			<td class="resultHeader">
				Woonplaats
			</td>
			<td class="resultHeader">
				Postcode
			</td>
			<td class="resultHeader">
				Bijzondere aansluiting
			</td>
			<td class="resultHeader">
				Markt segment
			</td>
			<td class="resultHeader">
				Naam Netbeheerder
			</td>
			<td class="resultHeader">
				Ean Netbeheerder
			</td>
		</tr>
		<tr height="1">
			<td bgcolor="#FFFFFF" colspan="10"></td>
		</tr>
		<tr height="1">
			<td bgcolor="#FEEAD3" colspan="10"></td>
		</tr>
		<tr height="2">
			<td colspan="4"></td>
		</tr>
			<tr bgcolor="#E5F0E7">
	
			<td class="resultData">
				871687140007253845			</td>
			<td class="resultData">
				Pittelderstraat			</td>
			<td class="resultData">
				8			</td>
			<td class="resultData">
									&nbsp;
							</td>
			<td class="resultData">
				DIDAM			</td>
			<td class="resultData">
				6942GJ			</td>
			<td class="resultData">
				NEE			</td>
			<td class="resultData">
									GAS
							</td>
			<td class="resultData">
				Liander NB			</td>
			<td class="resultData">
				8716871000002			</td>
		</tr>
			</tbody></table>

我想使用结果数据特别是生成的字符串“Liander NB”我希望从这个结果中得到一个变量,如$ beheerder =“Liander NB”;但我不知道如何从生成的CURL数据中生成变量。

2 个答案:

答案 0 :(得分:2)

使用CURLOPT_RETURNTRANSFER选项使curl_exec()返回回复。

curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
$result = curl_exec ($ch); 

然后,您可以使用DOMDocument来解析响应中的HTML及其在HTML中查找所需信息的方法。

答案 1 :(得分:0)

感谢Barmar,我已经添加了返回转移。 在PHPSimpleDOM解析器中使用了稍微不同的方法,但结果是一样的,我发现与DomDocument相比,它更容易使用。

<?php
$postcode     = ($_POST['postcode']);
$huisnummer   = ($_POST['huisnummer']);
$marktsegment = "27";
$aansluiting  = "-";
$url          = "xxxxxxxxx";
include('simple_html_dom.php');


$postdata = "postcode=" . $postcode . "&huisnummer=" . $huisnummer . "&marktsegment=" . $marktsegment . "&aansluiting=" . $aansluiting;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);


// Create a DOM object
$dom = new simple_html_dom();
// Load HTML from a string
$dom->load(curl_exec($ch));

$gasleverancier = $dom->find('td', 38)->plaintext; // zero based index selector

$gasleverancier = trim($gasleverancier);