简体   繁体   中英

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

 <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"; But I don't know how to make a variable out of the resulting CURL data.

Use the CURLOPT_RETURNTRANSFER option to make curl_exec() return the response.

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.

Thanks Barmar, I've added the 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.

<?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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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