7 مثال کاربردی با Curl و PHP

Mr.Steres

Active Member
CURL يك كتابخانه كامل و قوي در PHP هست كه توسط آقاي Daniel Stenberg پايه ريزي و تكميل شده. با استفاده از CURL شما مي تونيد به سرورهاي مختلف و با پروتكل هاي مختلف مثل http, https, ftp, gopher, telnet و ... كانكت بشين. شما با شناخت CURL و كتابخانه هاي قدرتمند ديگه PHP، هيچوقت از اينكه تصميم گرفتيد PHP كار بشيد، پشيمون نميشيد.

1 .بروزرسانی وضعیت کاربری در فیس بوک !

PHP:
<?PHP
/*******************************
*	Facebook Status Updater
*	Christian Flickinger
*	http://nexdot.net/blog
*	April 20, 2007
*******************************/

$status = 'YOUR_STATUS';
$first_name = 'YOUR_FIRST_NAME';
$login_email = 'YOUR_LOGIN_EMAIL';
$login_pass = 'YOUR_PASSWORD';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://login.facebook.com/login.php?m&amp;next=http%3A%2F%2Fm.facebook.com%2Fhome.php');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($login_email).'&pass='.urlencode($login_pass).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_exec($ch);

curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/home.php');
$page = curl_exec($ch);

curl_setopt($ch, CURLOPT_POST, 1);
preg_match('/name="post_form_id" value="(.*)" \/>'.ucfirst($first_name).'/', $page, $form_id);
curl_setopt($ch, CURLOPT_POSTFIELDS,'post_form_id='.$form_id[1].'&status='.urlencode($status).'&update=Update');
curl_setopt($ch, CURLOPT_URL, 'http://m.facebook.com/home.php');
curl_exec($ch);
?>

منبع : http://codesnippets.joyent.com/posts/show/1204

2. دریافت سرعت دانلود سرور خارجی

PHP:
<?php error_reporting(E_ALL | E_STRICT);

// Initialize cURL with given url
$url = 'http://download.bethere.co.uk/images/61859740_3c0c5dbc30_o.jpg';
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Sitepoint Examples (thread 581410; http://www.sitepoint.com/forums/showthread.php?t=581410)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);

set_time_limit(65);

$execute = curl_exec($ch);
$info = curl_getinfo($ch);

// Time spent downloading, I think
$time = $info['total_time']
      - $info['namelookup_time']
      - $info['connect_time']
      - $info['pretransfer_time']
      - $info['starttransfer_time']
      - $info['redirect_time'];

// Echo friendly messages
header('Content-Type: text/plain');
printf("Downloaded %d bytes in %0.4f seconds.\n", $info['size_download'], $time);
printf("Which is %0.4f mbps\n", $info['size_download'] * 8 / $time / 1024 / 1024);
printf("CURL said %0.4f mbps\n", $info['speed_download'] * 8 / 1024 / 1024);

echo "\n\ncurl_getinfo() said:\n", str_repeat('-', 31 + strlen($url)), "\n";
foreach ($info as $label => $value)
{
	printf("%-30s %s\n", $label, $value);
}
?>

منبع : http://cowburn.info/2008/11/29/download-speed-php-curl

3. ورود به حساب Myspace

PHP:
<?php

function login( $data, $useragent = 'Mozilla 4.01', $proxy = false ) {
    $ch = curl_init();
    $hash = crc32( $data['email'].$data['pass'] );
    $hash = sprintf( "%u", $hash );
    $randnum = $hash.rand( 0, 9999999 );
    if( $proxy ) curl_setopt( $ch, CURLOPT_PROXY, $proxy );
    curl_setopt( $ch, CURLOPT_COOKIEJAR, '/tmp/cookiejar-'.$randnum );
    curl_setopt( $ch, CURLOPT_COOKIEFILE, '/tmp/cookiejar-'.$randnum );
    curl_setopt( $ch, CURLOPT_USERAGENT, $useragent );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $ch, CURLOPT_POST, 0);
    curl_setopt( $ch, CURLOPT_URL, 'http://www.myspace.com' );
    $page = curl_exec( $ch );
    preg_match( '/MyToken=(.+?)"/i', $page, $token );
    if( $token[1] ) {
        curl_setopt( $ch, CURLOPT_URL, 'http://login.myspace.com/index.cfm?fuseaction=login.process&MyToken='.$token[1] );
        curl_setopt( $ch, CURLOPT_REFERER, 'http://www.myspace.com' );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, Array( 'Content-Type: application/x-www-form-urlencoded' ) );
        curl_setopt( $ch, CURLOPT_POST, 1 );
        $postfields = 'NextPage=&email='.urlencode( $data['mail'] ).'&password='.urlencode( $data['pass'] ).'&loginbutton.x=&loginbutton.y=';
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $postfields );
        $page = curl_exec( $ch );
        if( strpos( $page, 'SignOut' ) !== false ) {
                return $randnum;
        }
        else {
            preg_match( '/MyToken=(.+?)"/i', $page, $token );
            preg_match( '/replace\("([^\"]+)"/', $page, $redirpage );
            if( $token[1] ) {
                curl_setopt( $ch, CURLOPT_POST, 0 );
                curl_setopt( $ch, CURLOPT_URL, 'http://home.myspace.com/index.cfm?&fuseaction=user&Mytoken='.$token[1] );
                $page = curl_exec( $ch );
                curl_close( $ch );
                if( strpos( $page, 'SignOut' ) !== false ) {
                    return $randnum;
                }
            }
            elseif( $redirpage[1] ) {
                curl_setopt( $ch, CURLOPT_REFERER, 'http://login.myspace.com/index.cfm?fuseaction=login.process&MyToken='.$token[1] );
                curl_setopt( $ch, CURLOPT_URL, $redirpage[1] );
                curl_setopt( $ch, CURLOPT_POST, 0 );
                $page = curl_exec( $ch );
                curl_close( $ch );
                if( strpos( $page, 'SignOut' ) !== false ) {
                    return $randnum;
                }
            }
        }
    }
    return false;
}
?>

منبع : http://www.seo-blackhat.com/article/myspace-login-function-php-curl.html

4. ارسال یک پست در وبلاگ شما در وردپرس !

PHP:
function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords='',$encoding='UTF-8')
{
    $title = htmlentities($title,ENT_NOQUOTES,$encoding);
    $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);

    $content = array(
        'title'=>$title,
        'description'=>$body,
        'mt_allow_comments'=>0,  // 1 to allow comments
        'mt_allow_pings'=>0,  // 1 to allow trackbacks
        'post_type'=>'post',
        'mt_keywords'=>$keywords,
        'categories'=>array($category)
    );
    $params = array(0,$username,$password,$content,true);
    $request = xmlrpc_encode_request('metaWeblog.newPost',$params);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_URL, $rpcurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    $results = curl_exec($ch);
    curl_close($ch);
    return $results;
}

منبع : None

5. ارسال یک تویت به توییتر

PHP:
<?php
// Set username and password
$username = 'username';
$password = 'password';
// The message you want to send
$message = 'is twittering from php using curl';
// The twitter API address
$url = 'http://twitter.com/statuses/update.xml';
// Alternative JSON version
// $url = 'http://twitter.com/statuses/update.json';
// Set up and execute the curl process
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
// check for success or failure
if (empty($buffer)) {
    echo 'message';
} else {
    echo 'success';
}
?>

منبع : http://morethanseven.net/2007/01/20/posting-to-twitter-using-php/

6. دریافت سورس یک پیج و ریختن آن درون یک متغییر !

PHP:
<?php
    ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "example.com");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
?>

7. ارسال کامنت به وبلاگ های وردپرس ...

PHP:
<?php
$postfields = array();
$postfields["action"] = "submit";
$postfields["author"] = "Spammer";
$postfields["email"] = "[email protected]";
$postfields["url"] = "http://www.iamaspammer.com/";
$postfields["comment"] = "I am a stupid spammer.";
$postfields["comment_post_ID"] = "123";
$postfields["_wp_unfiltered_html_comment"] = "0d870b294b";
//Url of the form submission
$url = "http://www.ablogthatdoesntexist.com/blog/suggerer_site.php?action=meta_pass&id_cat=0";
$useragent = "Mozilla/5.0";
$referer = $url; 

//Initialize CURL session
$ch = curl_init($url);
//CURL options
curl_setopt($ch, CURLOPT_POST, 1);
//We post $postfields data
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
//We define an useragent (Mozilla/5.0)
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
//We define a refferer ($url)
curl_setopt($ch, CURLOPT_REFERER, $referer);
//We get the result page in a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//We exits CURL
$result = curl_exec($ch);
curl_close($ch);

//Finally, we display the result
echo $result;
?>

منبع : http://www.catswhocode.com/blog/how-spammers-spams-your-blog-comments

توسط Mr.steres
 
آخرین ویرایش:

datasun

Member
معرکه بود اما چند تا سوال ، چجوری میتونیم مطلب یه سایت رو بگیرم و اونو توی یه سیستم مدیریت محتوا پست کنیم ؟ به صورت خودکار

ممنون
 

Mr.Steres

Active Member
خوب اگه بخوام کلی بگم یک حلقه درست می کنی که محتوی وب سایت رو بخونه با کورل و با reqular expression محتوی رو که می خوای رو انتخاب می کنی و مثلا از همین کدی که توی این صفحه هست به وردپرس ارسال می کنی ... روشی هست که به ذهن من می رسه و شاید بهتر از این هم وجود داشته باشه ... تازه با rss هم میشه ...
 

datasun

Member
خوب اگه بخوام کلی بگم یک حلقه درست می کنی که محتوی وب سایت رو بخونه با کورل و با reqular expression محتوی رو که می خوای رو انتخاب می کنی و مثلا از همین کدی که توی این صفحه هست به وردپرس ارسال می کنی ... روشی هست که به ذهن من می رسه و شاید بهتر از این هم وجود داشته باشه ... تازه با rss هم میشه ...


خیلی ممنون آره با rss هم میشه :rose:
 

masodjavanmard

New Member
سلام
چطور میشه یک div خاص رو از یه صفحه سایت خارجی دریافت کنم وبدون استایلش تو سایت خودم نشون بدم؟
 

masodjavanmard

New Member
البته من با اين كد ميتونم به مطالب يه صفحه رو دريافت كنم ولي ميخوام فقط یه div خاص رو نمایش بدم اینکه استایلش اعمال شه
PHP:
    $site = curl_init();
    curl_setopt($site, CURLOPT_URL, "http://localhost/aleaaqeel/social/index.php/47-super-user/profile");
    curl_setopt($site, CURLOPT_RETURNTRANSFER, 1);
    $show = curl_exec($site);
    curl_close($site);
    echo $show;
 

masodjavanmard

New Member
يه سوال ديگه چطور میشه ارورها رو مخفی؟ وقتی به یه صفحه وصل میشه چندتا ارور مربوط بهش رو هم نمایش میده
 

cyberina

New Member
يه سوال ديگه چطور میشه ارورها رو مخفی؟ وقتی به یه صفحه وصل میشه چندتا ارور مربوط بهش رو هم نمایش میده
در اولین خط کدها , قبل از هر تابعی کد زیر رو بزنید
PHP:
error_reporting(0);
ضمنا میتونید برای غیر فعال کردن اروری که از یه تابع برگردونده شده از @ در ابتدای اسم تابع هنگام صدا زدن استفاده کنید.
 

جدیدترین ارسال ها

بالا