1

I am a complete newbie to php curl. Can anyone tell me where to start? I need to import some information from another web page and store it in my database after some modifications.

Roy Lee
  • 10,572
  • 13
  • 60
  • 84
saur
  • 103
  • 1
  • 3
  • 12
  • Start from the [manual](http://php.net/manual/en/curl.examples-basic.php). – Jon Jun 21 '12 at 09:56
  • possible duplicate of [How to parse and process HTML with PHP?](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php) – Madara's Ghost Jun 21 '12 at 09:57

5 Answers5

0

This is a great tutor you can get everything from here...

http://www.alfredfrancis.in/complete-php-curl-tutorial/

2.Simple cUrl scrit to download a web page.

<?php
 $ch = curl_init();//starts curl handle
 $Url="http://www.php.net";
 curl_setopt($ch, CURLOPT_URL, $Url);//set url to download
 curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com/");//referrer
 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");//set user agent
 curl_setopt($ch, CURLOPT_HEADER, 0);//include header in result
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//should return data
 curl_setopt($ch, CURLOPT_TIMEOUT, 20);//timeout in seconds
 $output = curl_exec($ch);//ececute the request
 curl_close($ch);
 echo $output;//output the result
?>
Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
0

Start: http://php.net/manual/en/curl.examples-basic.php

If you need to retrieve the output and manipulate it, just use the curl_setopt function, as curl_setopt(CURLOPT_RETURNTRANSFER,TRUE);.

Example where we retrieve the output from http://www.google.com and output it on the screen:

$ch = curl_init('http://www.google.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);

$output = curl_exec($ch);
var_dump($output);
dmmd
  • 2,938
  • 4
  • 33
  • 41
  • Hey hi @jlcd. I executed your code to retrieve output from google.com but while executing on the browser, I get the message bool(false) and nothing else..What am I doing wrong? – saur Jun 22 '12 at 04:48
  • Sorry, @user1458514, I forgot to include the `$ch` parameter on `curl_setopt`. I edited the answer, can you please try again? – dmmd Jun 23 '12 at 14:16
  • i already included that.. still getting the same result: bool(false) .. Any suggestions? – saur Jun 25 '12 at 09:18
  • You probably don't have the cURL PHP extension and/or cURL in your system. If you're under linux, just run `sudo apt-get install curl php5-curl` and restart your apache (`sudo /etc/init.d/apache2 restart` – dmmd Jun 25 '12 at 09:40
  • hey i have curl enabled(followed all the steps for installing and configuring curl). Windows XP is my OS..still nothing coming out!! – saur Jun 25 '12 at 11:12
0

Easy to debug!

i.e.

    $curl = curl_init();
    $URL="http://www.php.net";
    curl_setopt($curl, CURLOPT_URL, $URL);
    $contents = curl_exec($curl);
    $httpcode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
    $httpHeaders = curl_getinfo($curl);
    curl_close($curl);

    if($httpcode  && $contents!='')
    {
        makeLog('calls ', $URL.' resp c:'.$httpcode.' r:'.$contents); 
    }
    else if($httpcode)
    {
        makeLog('calls ', $URL.' resp c:'.$httpcode); 
    }

    function makeLog($function , $message='')
    {
        $myFile = "errorLogs.txt";
        $fh = fopen($myFile, 'a+');
        fwrite($fh , "\n\r\n\r".$_SERVER['REMOTE_ADDR'].': '.$message.' -'.$function."\n\r\n\r");
    }
Waqar Alamgir
  • 9,828
  • 4
  • 30
  • 36
0

Before you start doing anything with PHP curl, do you have it installed on the machine you are executing your .php files on?

If not:

sudo apt-get install curl libcurl3 libcurl3-dev php5-curl

Then restart apache:

service apache2 restart

After that place the following code into a .php file on your server and execute it. If it works, you should see your IP address in the top right corner. Try changing the URL to other websites. If you need more advice the whole article is here: Beginners-cURL

<?php

// Initialize cURL
$ch = curl_init();

// Set the website you would like to scrape
curl_setopt($ch, CURLOPT_URL, "http://www.icanhazip.com");

// Set cURL to return the results into a PHP variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// This executes the cURL request and places the results into a variable.
$curlResults= curl_exec($ch);

// Close curl
curl_close($ch);

// Echo the results to the screen>
echo $curlResults;

?>
Joshua
  • 126
  • 6
0

I know its not exactly what you asked for but there is an excellent PHP class that offers almost everything you require without the need to use cURL.

Its called Snoopy and is basically a browser emulating class with some very useful functionality like form data manipulation etc.