Why Can't I get That url link with cURL without integer value? [duplicate]












1
















This question already has an answer here:




  • How Do You Parse and Process HTML/XML in PHP?

    29 answers




I got the integer value that how much money in that page. But i want to get product name, url and price. How can i got it? How can i fix the problem?



 if (isset($_POST['search'])) {
$search = $_POST['search'];
$search = preg_replace("#[^0-9a-z]#", "", $search);
$url = "https://ryanscomputers.com/catalogsearch/result/?q='.$search";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);

preg_match_all('/<span class="price">(.*?)</span>/', $result, $matches);
$info = array_values(array_unique($matches[0]));
for ($i=0; $i < count($info); $i++){
echo $info[$i];
}

preg_match_all('!<h2 class="product-name"><a href="[^s]*?"!', $result, $matches);
$infourl = array_values(array_unique($matches[0]));
for ($i=0; $i < count ($infourl); $i++) {
echo $infourl[$i];
}
curl_close($curl);
}









share|improve this question















marked as duplicate by hanshenrik, Paul php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 23:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    1
















    This question already has an answer here:




    • How Do You Parse and Process HTML/XML in PHP?

      29 answers




    I got the integer value that how much money in that page. But i want to get product name, url and price. How can i got it? How can i fix the problem?



     if (isset($_POST['search'])) {
    $search = $_POST['search'];
    $search = preg_replace("#[^0-9a-z]#", "", $search);
    $url = "https://ryanscomputers.com/catalogsearch/result/?q='.$search";

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($curl);

    preg_match_all('/<span class="price">(.*?)</span>/', $result, $matches);
    $info = array_values(array_unique($matches[0]));
    for ($i=0; $i < count($info); $i++){
    echo $info[$i];
    }

    preg_match_all('!<h2 class="product-name"><a href="[^s]*?"!', $result, $matches);
    $infourl = array_values(array_unique($matches[0]));
    for ($i=0; $i < count ($infourl); $i++) {
    echo $infourl[$i];
    }
    curl_close($curl);
    }









    share|improve this question















    marked as duplicate by hanshenrik, Paul php
    Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 15 '18 at 23:27


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      1












      1








      1









      This question already has an answer here:




      • How Do You Parse and Process HTML/XML in PHP?

        29 answers




      I got the integer value that how much money in that page. But i want to get product name, url and price. How can i got it? How can i fix the problem?



       if (isset($_POST['search'])) {
      $search = $_POST['search'];
      $search = preg_replace("#[^0-9a-z]#", "", $search);
      $url = "https://ryanscomputers.com/catalogsearch/result/?q='.$search";

      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      $result = curl_exec($curl);

      preg_match_all('/<span class="price">(.*?)</span>/', $result, $matches);
      $info = array_values(array_unique($matches[0]));
      for ($i=0; $i < count($info); $i++){
      echo $info[$i];
      }

      preg_match_all('!<h2 class="product-name"><a href="[^s]*?"!', $result, $matches);
      $infourl = array_values(array_unique($matches[0]));
      for ($i=0; $i < count ($infourl); $i++) {
      echo $infourl[$i];
      }
      curl_close($curl);
      }









      share|improve this question

















      This question already has an answer here:




      • How Do You Parse and Process HTML/XML in PHP?

        29 answers




      I got the integer value that how much money in that page. But i want to get product name, url and price. How can i got it? How can i fix the problem?



       if (isset($_POST['search'])) {
      $search = $_POST['search'];
      $search = preg_replace("#[^0-9a-z]#", "", $search);
      $url = "https://ryanscomputers.com/catalogsearch/result/?q='.$search";

      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      $result = curl_exec($curl);

      preg_match_all('/<span class="price">(.*?)</span>/', $result, $matches);
      $info = array_values(array_unique($matches[0]));
      for ($i=0; $i < count($info); $i++){
      echo $info[$i];
      }

      preg_match_all('!<h2 class="product-name"><a href="[^s]*?"!', $result, $matches);
      $infourl = array_values(array_unique($matches[0]));
      for ($i=0; $i < count ($infourl); $i++) {
      echo $infourl[$i];
      }
      curl_close($curl);
      }




      This question already has an answer here:




      • How Do You Parse and Process HTML/XML in PHP?

        29 answers








      php curl web-development-server






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 14:33









      stefo91

      409




      409










      asked Nov 15 '18 at 13:54









      Siam Al MahmudSiam Al Mahmud

      66




      66




      marked as duplicate by hanshenrik, Paul php
      Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 15 '18 at 23:27


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by hanshenrik, Paul php
      Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 15 '18 at 23:27


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          you should read up on how to parse XML/HTML with PHP, because you are doing it very wrong. (do not parse html with regex)



          that said, this can be parsed out rather easily with DOMDocument + DOMXPath, check



          <?php
          header("content-type: text/plain;charset=utf-8");
          $ch = curl_init('https://ryanscomputers.com/catalogsearch/result/?q=cat');
          curl_setopt_array($ch, array(
          CURLOPT_ENCODING => '',
          CURLOPT_RETURNTRANSFER => 1
          ));
          $html = curl_exec($ch);
          curl_close($ch);
          $domd = @DOMDocument::loadHTML($html);
          $xp = new DOMXPath($domd);
          foreach ($xp->query("//div[contains(@class,'item-inner')]") as $item) {
          //var_dump($domd->saveHTML($item));
          $name = $xp->query(".//h2[contains(@class,'product-name')]", $item)->item(0)->textContent;
          $name = trim($name);
          $price = $xp->query(".//div[contains(@class,'price-box')]", $item)->item(0)->textContent;
          $price = trim(preg_replace('/s+/', ' ',$price));
          $url=$item->getElementsByTagName("a")->item(0)->getAttribute("href");
          print_r([
          'name' => $name,
          'price' => $price,
          'url' => $url,
          ]);
          }


          output:



          Array
          (
          [name] => Black Cat BC-01 Laptop Cooler
          [price] => Regular Price Tk 660 Special Price Tk 650
          [url] => https://ryanscomputers.com/black-cat-bc-01-laptop-cooler.html
          )
          Array
          (
          [name] => Black Cat BC-02 Laptop Cooler
          [price] => Regular Price Tk 660 Special Price Tk 650
          [url] => https://ryanscomputers.com/black-cat-bc-02-laptop-cooler.html
          )
          Array
          (
          [name] => Black Cat BC-06 Laptop Cooler
          [price] => Regular Price Tk 910 Special Price Tk 900
          [url] => https://ryanscomputers.com/black-cat-bc-06-laptop-cooler.html
          )
          Array
          (
          [name] => Black Cat BC-07 Laptop Cooler
          [price] => Regular Price Tk 910 Special Price Tk 900
          [url] => https://ryanscomputers.com/black-cat-bc-07-laptop-cooler.html
          )
          Array
          (
          [name] => Black Cat BC-05 Notebook Cooler
          [price] => Regular Price Tk 720 Special Price Tk 700
          [url] => https://ryanscomputers.com/black-cat-bc-05-notebook-cooler.html
          )
          Array
          (
          [name] => Black Cat K-680 Mini USB Keyboard
          [price] => Regular Price Tk 420 Special Price Tk 400
          [url] => https://ryanscomputers.com/black-cat-k-680-mini-usb-keyboard.html
          )
          ~~truncated





          share|improve this answer
































            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            you should read up on how to parse XML/HTML with PHP, because you are doing it very wrong. (do not parse html with regex)



            that said, this can be parsed out rather easily with DOMDocument + DOMXPath, check



            <?php
            header("content-type: text/plain;charset=utf-8");
            $ch = curl_init('https://ryanscomputers.com/catalogsearch/result/?q=cat');
            curl_setopt_array($ch, array(
            CURLOPT_ENCODING => '',
            CURLOPT_RETURNTRANSFER => 1
            ));
            $html = curl_exec($ch);
            curl_close($ch);
            $domd = @DOMDocument::loadHTML($html);
            $xp = new DOMXPath($domd);
            foreach ($xp->query("//div[contains(@class,'item-inner')]") as $item) {
            //var_dump($domd->saveHTML($item));
            $name = $xp->query(".//h2[contains(@class,'product-name')]", $item)->item(0)->textContent;
            $name = trim($name);
            $price = $xp->query(".//div[contains(@class,'price-box')]", $item)->item(0)->textContent;
            $price = trim(preg_replace('/s+/', ' ',$price));
            $url=$item->getElementsByTagName("a")->item(0)->getAttribute("href");
            print_r([
            'name' => $name,
            'price' => $price,
            'url' => $url,
            ]);
            }


            output:



            Array
            (
            [name] => Black Cat BC-01 Laptop Cooler
            [price] => Regular Price Tk 660 Special Price Tk 650
            [url] => https://ryanscomputers.com/black-cat-bc-01-laptop-cooler.html
            )
            Array
            (
            [name] => Black Cat BC-02 Laptop Cooler
            [price] => Regular Price Tk 660 Special Price Tk 650
            [url] => https://ryanscomputers.com/black-cat-bc-02-laptop-cooler.html
            )
            Array
            (
            [name] => Black Cat BC-06 Laptop Cooler
            [price] => Regular Price Tk 910 Special Price Tk 900
            [url] => https://ryanscomputers.com/black-cat-bc-06-laptop-cooler.html
            )
            Array
            (
            [name] => Black Cat BC-07 Laptop Cooler
            [price] => Regular Price Tk 910 Special Price Tk 900
            [url] => https://ryanscomputers.com/black-cat-bc-07-laptop-cooler.html
            )
            Array
            (
            [name] => Black Cat BC-05 Notebook Cooler
            [price] => Regular Price Tk 720 Special Price Tk 700
            [url] => https://ryanscomputers.com/black-cat-bc-05-notebook-cooler.html
            )
            Array
            (
            [name] => Black Cat K-680 Mini USB Keyboard
            [price] => Regular Price Tk 420 Special Price Tk 400
            [url] => https://ryanscomputers.com/black-cat-k-680-mini-usb-keyboard.html
            )
            ~~truncated





            share|improve this answer






























              0














              you should read up on how to parse XML/HTML with PHP, because you are doing it very wrong. (do not parse html with regex)



              that said, this can be parsed out rather easily with DOMDocument + DOMXPath, check



              <?php
              header("content-type: text/plain;charset=utf-8");
              $ch = curl_init('https://ryanscomputers.com/catalogsearch/result/?q=cat');
              curl_setopt_array($ch, array(
              CURLOPT_ENCODING => '',
              CURLOPT_RETURNTRANSFER => 1
              ));
              $html = curl_exec($ch);
              curl_close($ch);
              $domd = @DOMDocument::loadHTML($html);
              $xp = new DOMXPath($domd);
              foreach ($xp->query("//div[contains(@class,'item-inner')]") as $item) {
              //var_dump($domd->saveHTML($item));
              $name = $xp->query(".//h2[contains(@class,'product-name')]", $item)->item(0)->textContent;
              $name = trim($name);
              $price = $xp->query(".//div[contains(@class,'price-box')]", $item)->item(0)->textContent;
              $price = trim(preg_replace('/s+/', ' ',$price));
              $url=$item->getElementsByTagName("a")->item(0)->getAttribute("href");
              print_r([
              'name' => $name,
              'price' => $price,
              'url' => $url,
              ]);
              }


              output:



              Array
              (
              [name] => Black Cat BC-01 Laptop Cooler
              [price] => Regular Price Tk 660 Special Price Tk 650
              [url] => https://ryanscomputers.com/black-cat-bc-01-laptop-cooler.html
              )
              Array
              (
              [name] => Black Cat BC-02 Laptop Cooler
              [price] => Regular Price Tk 660 Special Price Tk 650
              [url] => https://ryanscomputers.com/black-cat-bc-02-laptop-cooler.html
              )
              Array
              (
              [name] => Black Cat BC-06 Laptop Cooler
              [price] => Regular Price Tk 910 Special Price Tk 900
              [url] => https://ryanscomputers.com/black-cat-bc-06-laptop-cooler.html
              )
              Array
              (
              [name] => Black Cat BC-07 Laptop Cooler
              [price] => Regular Price Tk 910 Special Price Tk 900
              [url] => https://ryanscomputers.com/black-cat-bc-07-laptop-cooler.html
              )
              Array
              (
              [name] => Black Cat BC-05 Notebook Cooler
              [price] => Regular Price Tk 720 Special Price Tk 700
              [url] => https://ryanscomputers.com/black-cat-bc-05-notebook-cooler.html
              )
              Array
              (
              [name] => Black Cat K-680 Mini USB Keyboard
              [price] => Regular Price Tk 420 Special Price Tk 400
              [url] => https://ryanscomputers.com/black-cat-k-680-mini-usb-keyboard.html
              )
              ~~truncated





              share|improve this answer




























                0












                0








                0







                you should read up on how to parse XML/HTML with PHP, because you are doing it very wrong. (do not parse html with regex)



                that said, this can be parsed out rather easily with DOMDocument + DOMXPath, check



                <?php
                header("content-type: text/plain;charset=utf-8");
                $ch = curl_init('https://ryanscomputers.com/catalogsearch/result/?q=cat');
                curl_setopt_array($ch, array(
                CURLOPT_ENCODING => '',
                CURLOPT_RETURNTRANSFER => 1
                ));
                $html = curl_exec($ch);
                curl_close($ch);
                $domd = @DOMDocument::loadHTML($html);
                $xp = new DOMXPath($domd);
                foreach ($xp->query("//div[contains(@class,'item-inner')]") as $item) {
                //var_dump($domd->saveHTML($item));
                $name = $xp->query(".//h2[contains(@class,'product-name')]", $item)->item(0)->textContent;
                $name = trim($name);
                $price = $xp->query(".//div[contains(@class,'price-box')]", $item)->item(0)->textContent;
                $price = trim(preg_replace('/s+/', ' ',$price));
                $url=$item->getElementsByTagName("a")->item(0)->getAttribute("href");
                print_r([
                'name' => $name,
                'price' => $price,
                'url' => $url,
                ]);
                }


                output:



                Array
                (
                [name] => Black Cat BC-01 Laptop Cooler
                [price] => Regular Price Tk 660 Special Price Tk 650
                [url] => https://ryanscomputers.com/black-cat-bc-01-laptop-cooler.html
                )
                Array
                (
                [name] => Black Cat BC-02 Laptop Cooler
                [price] => Regular Price Tk 660 Special Price Tk 650
                [url] => https://ryanscomputers.com/black-cat-bc-02-laptop-cooler.html
                )
                Array
                (
                [name] => Black Cat BC-06 Laptop Cooler
                [price] => Regular Price Tk 910 Special Price Tk 900
                [url] => https://ryanscomputers.com/black-cat-bc-06-laptop-cooler.html
                )
                Array
                (
                [name] => Black Cat BC-07 Laptop Cooler
                [price] => Regular Price Tk 910 Special Price Tk 900
                [url] => https://ryanscomputers.com/black-cat-bc-07-laptop-cooler.html
                )
                Array
                (
                [name] => Black Cat BC-05 Notebook Cooler
                [price] => Regular Price Tk 720 Special Price Tk 700
                [url] => https://ryanscomputers.com/black-cat-bc-05-notebook-cooler.html
                )
                Array
                (
                [name] => Black Cat K-680 Mini USB Keyboard
                [price] => Regular Price Tk 420 Special Price Tk 400
                [url] => https://ryanscomputers.com/black-cat-k-680-mini-usb-keyboard.html
                )
                ~~truncated





                share|improve this answer















                you should read up on how to parse XML/HTML with PHP, because you are doing it very wrong. (do not parse html with regex)



                that said, this can be parsed out rather easily with DOMDocument + DOMXPath, check



                <?php
                header("content-type: text/plain;charset=utf-8");
                $ch = curl_init('https://ryanscomputers.com/catalogsearch/result/?q=cat');
                curl_setopt_array($ch, array(
                CURLOPT_ENCODING => '',
                CURLOPT_RETURNTRANSFER => 1
                ));
                $html = curl_exec($ch);
                curl_close($ch);
                $domd = @DOMDocument::loadHTML($html);
                $xp = new DOMXPath($domd);
                foreach ($xp->query("//div[contains(@class,'item-inner')]") as $item) {
                //var_dump($domd->saveHTML($item));
                $name = $xp->query(".//h2[contains(@class,'product-name')]", $item)->item(0)->textContent;
                $name = trim($name);
                $price = $xp->query(".//div[contains(@class,'price-box')]", $item)->item(0)->textContent;
                $price = trim(preg_replace('/s+/', ' ',$price));
                $url=$item->getElementsByTagName("a")->item(0)->getAttribute("href");
                print_r([
                'name' => $name,
                'price' => $price,
                'url' => $url,
                ]);
                }


                output:



                Array
                (
                [name] => Black Cat BC-01 Laptop Cooler
                [price] => Regular Price Tk 660 Special Price Tk 650
                [url] => https://ryanscomputers.com/black-cat-bc-01-laptop-cooler.html
                )
                Array
                (
                [name] => Black Cat BC-02 Laptop Cooler
                [price] => Regular Price Tk 660 Special Price Tk 650
                [url] => https://ryanscomputers.com/black-cat-bc-02-laptop-cooler.html
                )
                Array
                (
                [name] => Black Cat BC-06 Laptop Cooler
                [price] => Regular Price Tk 910 Special Price Tk 900
                [url] => https://ryanscomputers.com/black-cat-bc-06-laptop-cooler.html
                )
                Array
                (
                [name] => Black Cat BC-07 Laptop Cooler
                [price] => Regular Price Tk 910 Special Price Tk 900
                [url] => https://ryanscomputers.com/black-cat-bc-07-laptop-cooler.html
                )
                Array
                (
                [name] => Black Cat BC-05 Notebook Cooler
                [price] => Regular Price Tk 720 Special Price Tk 700
                [url] => https://ryanscomputers.com/black-cat-bc-05-notebook-cooler.html
                )
                Array
                (
                [name] => Black Cat K-680 Mini USB Keyboard
                [price] => Regular Price Tk 420 Special Price Tk 400
                [url] => https://ryanscomputers.com/black-cat-k-680-mini-usb-keyboard.html
                )
                ~~truncated






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 '18 at 23:58

























                answered Nov 15 '18 at 23:11









                hanshenrikhanshenrik

                10.4k21840




                10.4k21840

















                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python