Php curl mã hóa url

Sự nhìn nhận

Bạn mới sử dụng cURL?

  • trang Wikipedia cURL
  • hướng dẫn cURL. Sử dụng cURL để tự động hóa các công việc HTTP

Xin lưu ý rằng một số kỹ thuật được trình bày ở đây có thể được sử dụng cho các phương pháp “mũ đen”. Mục tiêu của bài viết này chỉ mang tính giáo dục, vui lòng không sử dụng bất kỳ đoạn trích nào bên dưới cho mục đích bất hợp pháp

1 – Cập nhật trạng thái Facebook của bạn

Muốn cập nhật trạng thái facebook nhưng không muốn vào facebook. com, đăng nhập và cuối cùng là có thể cập nhật trạng thái của mình?

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

2 – Nhận tốc độ tải xuống của máy chủ web của bạn

Bạn có bao giờ muốn biết tốc độ tải xuống chính xác của máy chủ web của mình (hoặc bất kỳ máy chủ nào khác không?) Nếu có, bạn sẽ thích đoạn mã đó. Bạn chỉ cần khởi tạo biến $url với bất kỳ tài nguyên nào từ máy chủ web (hình ảnh, pdf, v.v.), đặt tệp trên máy chủ của bạn và trỏ trình duyệt của bạn tới đó. Đầu ra sẽ là một báo cáo đầy đủ về tốc độ tải xuống

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

Nguồn. http. // cao bồi. info/2008/11/29/download-speed-php-curl

3 – Đăng nhập Myspace bằng cURL

<?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;
}
?>

Nguồn. http. //www. seo-blackhat. com/article/myspace-login-function-php-curl. html

4 – Xuất bản một bài đăng trên blog WordPress của bạn, sử dụng cURL

Tôi biết rằng hầu hết các bạn đều thích WordPress, vì vậy đây là một "mẹo" hay mà tôi thường đăng trên blog khác của mình WpRecipes
Chức năng này có thể đăng lên blog WordPress của bạn. Bạn không cần phải đăng nhập vào bảng điều khiển WP của mình, v.v.
Mặc dù vậy, bạn phải kích hoạt tùy chọn đăng bài XMLRPC trong blog WordPress của mình. Nếu tùy chọn này không được kích hoạt, mã sẽ không thể chèn bất cứ thứ gì vào cơ sở dữ liệu WordPress. Một điều nữa, đảm bảo các chức năng XMLRPC được kích hoạt trên php của bạn. tập tin ini

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

5 – Kiểm tra sự tồn tại của một url nhất định

Tôi biết, nó nghe có vẻ cơ bản. Trên thực tế, nó là cơ bản nhưng cũng rất hữu ích, đặc biệt là khi bạn phải làm việc với các nguồn lực bên ngoài

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.jellyandcustard.com/");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch)
echo $data;
?>

Nguồn. http. //www. phpsnippets. info/test-existence-of-a-given-url-with-curl

6 – Đăng bình luận trên blog WordPress

Trong một bài viết trước, tôi đã thảo luận về cách những kẻ gửi thư rác gửi thư rác cho blog WordPress của bạn. Để làm như vậy, họ chỉ cần điền vào mảng $postfields thông tin họ muốn hiển thị và tải trang
Tất nhiên, mã này chỉ dành cho mục đích giáo dục

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

Nguồn. http. //www. mã mèo. com/blog/cach-spammers-spams-your-blog-comments

7 – Theo dõi thu nhập Adsense của bạn với trình đọc RSS

Hầu hết các blogger sử dụng Adsense trên blog của họ và (cố gắng) kiếm tiền với Google. Đoạn mã tuyệt vời này cho phép bạn theo dõi thu nhập Adsense của mình…bằng trình đọc RSS. Chắc chắn tuyệt vời
(Tập lệnh quá lớn để hiển thị trên blog, nhấp vào đây để xem trước)
Nguồn. http. // hành tinh. com/blog/my-projects/track-adsense-earnings-in-rss-feed/

8 – Nhận số người đăng ký nguồn cấp dữ liệu ở dạng toàn văn

Nếu bạn là một blogger, có lẽ bạn đang sử dụng dịch vụ FeedBurner phổ biến, dịch vụ này cho phép bạn biết có bao nhiêu người đã lấy nguồn cấp dữ liệu rss của bạn. Feedburner có một chicklet để tự hào hiển thị số lượng người đăng ký trên blog của bạn. Cá nhân tôi thích vẻ ngoài của chicklet, nhưng tôi nghe rất nhiều blogger phàn nàn về nó. may mắn thay, cURL có thể chỉ cần lấy giá trị đếm và trả về cho bạn dưới dạng một biến để bạn có thể hiển thị giá trị đó theo ý muốn trên blog của mình

//get cool feedburner count
$whaturl="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=feedburner-id";

//Initialize the Curl session
$ch = curl_init();

//Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//Set the URL
curl_setopt($ch, CURLOPT_URL, $whaturl);

//Execute the fetch
$data = curl_exec($ch);

//Close the connection
curl_close($ch);
$xml = new SimpleXMLElement($data);
$fb = $xml->feed->entry['circulation'];
//end get cool feedburner count

Nguồn. http. //www. Hồng Kông. com/blog/display-google-feed-subscriber-count-in-text/

9 – Lấy nội dung của một trang web vào một biến PHP

Đây là một điều rất cơ bản để làm với cURL, nhưng với khả năng vô tận. Khi bạn có một trang web trong một biến PHP, chẳng hạn, bạn có thể truy xuất một thông tin cụ thể trên trang để sử dụng trên trang web của riêng mình

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

 

10 – Đăng lên Twitter bằng PHP và cURL

Twitter đã rất phổ biến từ lâu và có thể bạn đã có tài khoản ở đó. (Chúng tôi cũng có một cái) Vậy còn việc sử dụng cURL để tweet từ máy chủ của bạn mà không cần kết nối với Twitter thì sao?