


CURL application in PHP and based on modified Google implementation
Aug 08, 2016 am 09:28 AM能做什么
<code>PHP中的curl庫最基本的用途就是去簡單有效地*抓取網(wǎng)頁的內(nèi)容*,然后利用抓取到的網(wǎng)頁數(shù)據(jù)經(jīng)程序的方式進(jìn)行處理和分析,最終得到你所想要的數(shù)據(jù)。當(dāng)然了,這是最基本的用法,還有其他復(fù)雜有趣的用途,這取決于你的curl_setopt()行數(shù)的設(shè)置 </code>
怎么用
<code>首先,你的PHP中必須安裝有curl的擴(kuò)展插件,即phpinfo中有如下顯示  然后就可以開始使用了,具體步驟: 1.用curl_init()初始化一個curl會話 2.通過curl_setopt()來設(shè)置需要的全部選項(xiàng) 3.用curl_exec()來執(zhí)行會話 4.執(zhí)行完后使用curl_close()關(guān)閉會話 </code>
ps:curl_setopt()是curl使用的關(guān)鍵,很多復(fù)雜的用法都是基于此
一個簡單的例子
<code><?php <span>//初始化一個curl對象</span><span>$curl</span>=curl_init(<span>"http://www.baidu.com"</span>); <span>//將抓取的數(shù)據(jù)已文本的形式保存</span><span>$fp</span>=<span>fopen</span>(<span>"test.txt"</span>); <span>//設(shè)置curl_setopt()參數(shù)</span> curl_setopt(<span>$ch</span>,CURLOPT_FILE,<span>$fp</span>); curl_setopt(<span>$ch</span>,CURLOPT_HEADER,<span>0</span>); <span>//執(zhí)行curl_exec();</span> curl_exec(<span>$ch</span>); <span>//關(guān)閉curl會話</span> curl_exec(<span>$ch</span>); curl_close(<span>$ch</span>); <span>fclose</span>(<span>$fp</span>);</code>
以上例子來自官方文檔
利用CURL實(shí)現(xiàn)抓取GOOGLE的host腳本
<code><?php <span>//初始化curl對話</span><span>$curl</span>=curl_init(); <span>//設(shè)置curl_setopt</span> curl_setopt(<span>$curl</span>, CURLOPT_URL, <span>'http://www.360kb.com/kb/2_122.html'</span>); curl_setopt(<span>$curl</span>,CURLOPT_HEADER,<span>0</span>); curl_setopt(<span>$curl</span>, CURLOPT_RETURNTRANSFER, true); <span>//執(zhí)行curl</span><span>$data</span>=curl_exec(<span>$curl</span>); <span>//關(guān)閉curl資源</span> curl_close(<span>$curl</span>); <span>//清楚HTML標(biāo)簽</span><span>$data</span>=strip_tags(<span>$data</span>); <span>//處理的讀入數(shù)據(jù)</span><span>$data</span>=substr(<span>$data</span>, strpos(<span>$data</span>, <span>'#google hosts'</span>)); <span>$data</span>=substr(<span>$data</span>,<span>0</span>,strpos(<span>$data</span>, <span>'#google hosts 2015 end'</span>)); <span>$data</span>=str_replace(<span>' '</span>, <span>' '</span>, <span>$data</span>); <span>//寫入host</span><span>$host</span>=file_get_contents(<span>"C:\\Windows\\System32\\drivers\\etc\\HOSTS"</span>); <span>$host</span>=substr(<span>$host</span>,<span>0</span>,strpos(<span>$host</span>, <span>'#google hosts'</span>)); <span>$host</span>.=<span>$data</span>; <span>$fh</span>=<span>fopen</span>(<span>"C:\\Windows\\System32\\drivers\\etc\\HOSTS"</span>, <span>'w'</span>); <span>fwrite</span>(<span>fopen</span>(<span>"C:\\Windows\\System32\\drivers\\etc\\HOSTS"</span>, <span>'w'</span>), <span>$host</span>); <span>fclose</span>(<span>$fh</span>); </code>
以上就介紹了PHP中CURL應(yīng)用與基于的修改Google實(shí)現(xiàn),包括了方面的內(nèi)容,希望對PHP教程有興趣的朋友有所幫助。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Recently, many Win11 users have reported that when shutting down, they are prompted that the taskhostwindow task host is executing the shutdown task. So what is going on? Users can enter the Desktop folder under the local registry editor, and then select AutoEndTasks in the right window to set it. Let this site carefully introduce to users the solution to this problem when shutting down. Windows 11 shutdown prompts that the taskhostwindow task host is executing the shutdown task. Solution 1. Use the key combination win key + r key, enter "regedit" and press Enter, as shown in the figure below. 2. Search for [HKEY

When we launch Task Manager to terminate a task or stop an application, we usually find a large number of processes running. This is completely normal. However, sometimes we see programs that are using system resources that we are completely unaware of. One of these processes is the aggregator host.exe, which has been causing some confusion among users recently. Some of these processes may be legitimate Windows requirements, but others may be malicious programs running in the background and causing problems without the user's knowledge or consent. After we've seen the five ways you can launch Task Manager in Windows 11, we'll show you how to check if aggregator host.exe is safe or a virus. keep up

The solutions to "no route to host" include checking the network connection, checking the IP address and port, checking the firewall configuration, checking the routing configuration, checking the network device configuration, checking the network service status, checking the network configuration and contacting the network administrator. Detailed introduction: 1. Check the network connection to ensure that the network connection between the client and the target host is normal. You can try to test network connectivity through the ping command or other network tools, and check whether hardware devices such as network cables, wireless networks, and routers are working properly. Make sure the network connection is stable, etc.

The differences between master and host are: 1. Host can play the role of client or server, while master is the central server responsible for coordinating and managing other slave servers in a distributed system; 2. Host is an ordinary computer device, and master usually has Higher processing power and resources are used to process and distribute tasks, manage data, and maintain the stability of the entire system; 3. The host is a node in the network, and the master is the server that plays a core role in the distributed system.

Both curl and Pythonrequests are powerful tools for sending HTTP requests. While curl is a command-line tool that allows you to send requests directly from the terminal, Python's requests library provides a more programmatic way to send requests from Python code. The basic syntax for converting curl to Pythonrequestscurl command is as follows: curl[OPTIONS]URL When converting curl command to Python request, we need to convert the options and URL into Python code. Here is an example curlPOST command: curl-XPOST https://example.com/api

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

PHP8.1 released: Introducing curl for concurrent processing of multiple requests. Recently, PHP officially released the latest version of PHP8.1, which introduced an important feature: curl for concurrent processing of multiple requests. This new feature provides developers with a more efficient and flexible way to handle multiple HTTP requests, greatly improving performance and user experience. In previous versions, handling multiple requests often required creating multiple curl resources and using loops to send and receive data respectively. Although this method can achieve the purpose

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check
