CURL functions to get source code of a url

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Amzul
    New Member
    • Oct 2007
    • 130

    CURL functions to get source code of a url

    hello all

    im trying to get the source code of a website,
    i mange to do this with curl
    snipt:

    [CODE=php]<?php
    $ch = curl_init("http ://somesite.com/with/page/url.html");
    $fp = fopen("/tmp/site.txt", "w+");
    curl_setopt($ch , CURLOPT_FILE, $fp);
    curl_setopt($ch , CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch) ;
    fclose($fp);
    $hand = fopen("/tmp/site.txt","r");
    $lines ="";
    while(!feof($ha nd)){
    $lines .= fgets($hand);
    }
    $code = trim(substr($li nes,strpos($lin es,"<a"),strpos ($lines,"</a>")));// i want <a></a> tag
    $herf =substr($code,1 0,100);// the numbers are bogus
    //at the end i am returring the tag or inserting it to a db
    fclose($hand);
    ?>
    [/CODE]

    this code is working fine but BUT
    i want to know is there a better way... i dont want my server to open and close file every time one of the guys will run the script
    is there a way to do it in the server memory to get a response faster?

    thanks head
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    When you mean that different user sessions have to write in SHARED storage, the answer is: not in PHP.

    Ronald

    Comment

    • Amzul
      New Member
      • Oct 2007
      • 130

      #3
      Originally posted by ronverdonk
      When you mean that different user sessions have to write in SHARED storage, the answer is: not in PHP.

      Ronald
      thats not what i ment but can you explian with what it can b possible.

      in my case i did this: instead of fopen i left it as a string
      snipt:

      [CODE=php]$ch = curl_init("www. somepageurl.com ");
      curl_setopt($ch , CURLOPT_RETURNT RANSFER, 1);
      curl_setopt($ch , CURLOPT_HEADER, 0);
      $file = curl_exec($ch);
      curl_close($ch) ;[/CODE]
      $file contain the string that i need (no head) and i can work with it

      Comment

      Working...