Table of Contents
一波PHP中cURL库的常见用法代码示例,一波curl
Home php教程 php手册 一波PHP中cURL库的常见用法代码示例,一波curl

一波PHP中cURL库的常见用法代码示例,一波curl

Jun 13, 2016 am 08:41 AM
curl php

一波PHP中cURL库的常见用法代码示例,一波curl

php 的CURL是不错的功能,下面收藏几段不错的片段

0、基本例子
一般流程:

$to_url=$_GET['url'];
print_r($_GET);
if(substr($to_url,0,1)=='/'){
 $to_url="http://www.amazon.com".$to_url;
}
echo $to_url;
//初始化
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $to_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//执行并获取HTML文档内容
$output = curl_exec($ch);
$output=preg_replace("#href=\"#","href=\"http://in2.qq-ex.com/amazon.php?url=",$output);
// 释放curl句柄
curl_close($ch);
echo $output;
// 指定代理地址
curl_setopt($ch, CURLOPT_PROXY, '11.11.11.11:8080');
// 如果需要的话,提供用户名和密码
curl_setopt($ch, CURLOPT_PROXYUSERPWD,'user:pass');
Copy after login

1、测试网站是否运行正常

 if (isDomainAvailible('http://gz.itownet.cn')) 
  { 
    echo "Up and running!"; 
  } 
  else 
  { 
    echo "Woops, nothing found there."; 
  } 
 
  //returns true, if domain is availible, false if not 
  function isDomainAvailible($domain) 
  { 
    //check, if a valid url is provided 
    if(!filter_var($domain, FILTER_VALIDATE_URL)) 
    { 
      return false; 
    } 
 
    //initialize curl 
    $curlInit = curl_init($domain); 
    curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10); 
    curl_setopt($curlInit,CURLOPT_HEADER,true); 
    curl_setopt($curlInit,CURLOPT_NOBODY,true); 
    curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true); 
 
    //get answer 
    $response = curl_exec($curlInit); 
 
    curl_close($curlInit); 
 
    if ($response) return true; 
 
    return false; 
  } 

Copy after login

2、可以代替file_gecontents的操作

function file_get_contents_curl($url) { 
 $ch = curl_init(); 
 
 curl_setopt($ch, CURLOPT_HEADER, 0); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser. 
 curl_setopt($ch, CURLOPT_URL, $url); 
 
 $data = curl_exec($ch); 
 curl_close($ch); 
 
 return $data; 
} 

Copy after login

3、保存某个网站下的所有图片

 function getImages($html) { 
 $matches = array(); 
 $regex = '~http://somedomain.com/images/(.*?)\.jpg~i'; 
 preg_match_all($regex, $html, $matches); 
 foreach ($matches[1] as $img) { 
  saveImg($img); 
 } 
} 
 
function saveImg($name) { 
 $url = 'http://somedomain.com/images/'.$name.'.jpg'; 
 $data = get_data($url); 
 file_put_contents('photos/'.$name.'.jpg', $data); 
} 
 
$i = 1; 
$l = 101; 
 
while ($i < $l) { 
 $html = get_data('http://somedomain.com/id/'.$i.'/'); 
 getImages($html); 
 $i += 1; 
} 

Copy after login

4、FTP应用

// open a file pointer 
$file = fopen("/path/to/file", "r"); 
 
// the url contains most of the info needed 
$url = "ftp://username:password@mydomain.com:21/path/to/new/file"; 
 
$ch = curl_init(); 
 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
 
// upload related options 
curl_setopt($ch, CURLOPT_UPLOAD, 1); 
curl_setopt($ch, CURLOPT_INFILE, $fp); 
curl_setopt($ch, CURLOPT_INFILESIZE, filesize("/path/to/file")); 
 
// set for ASCII mode (e.g. text files) 
curl_setopt($ch, CURLOPT_FTPASCII, 1); 
 
$output = curl_exec($ch); 
curl_close($ch); 

Copy after login

5、使用curl发送JSON数据

$data = array("name" => "Hagrid", "age" => "36");                                   
$data_string = json_encode($data);                                           
  
$ch = curl_init('http://api.local/rest/users');                                    
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                    
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                    
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                      
  'Content-Type: application/json',                                         
  'Content-Length: ' . strlen($data_string))                                     
);                                                           
  
$result = curl_exec($ch); 
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles