简体   繁体   English

将CURL结果保存到变量PHP中

[英]Save CURL result in a variable PHP

I'm new to the whole CURL library in PHP and managed to sucesfully get post data from a website using the following code: 我是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);
    ?>

Wich returns the following table: Wich返回下表:

 <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> 

I want to work with the resulting data specifically the resulting string "Liander NB" I want to make a var out of this result like so $beheerder ="Liander NB"; 我想使用结果数据,特别是结果字符串“ Liander NB”,我想从这个结果中得到一个变量,就像这样$ beheerder =“ Liander NB”; But I don't know how to make a variable out of the resulting CURL data. 但是我不知道如何从生成的CURL数据中创建变量。

Use the CURLOPT_RETURNTRANSFER option to make curl_exec() return the response. 使用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); 

Then you can use DOMDocument to parse the HTML in the response, and its methods to find the information you want in the HTML. 然后,您可以使用DOMDocument解析响应中的HTML,并使用其方法在HTML中查找所需的信息。

Thanks Barmar, I've added the returntransfer. 感谢Barmar,我添加了returntransfer。 used a slightly diffrent approach with the PHPSimpleDOM parser but the result is the same, I just find it easier to work with as opposed to DomDocument. 在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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM