0

I'm new to the whole CURL library in PHP and managed to successfully 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);
    ?>

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

1
  • 1
    you forgot to url encode the variables. you'll fetch the wrong url if say $aansluiting contains a space or & or <> special characters. check out urlencode() , or better yet, http_build_query Commented Jun 6, 2017 at 21:39

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.