与老涂一起写代码

如何写CURL访问网络?

admin 272 ℃ 0 条
如何写CURL访问网络?

CURL发送Get和Post请求:

function curl_request($url,$data=null,$method="GET"){
	if(is_array($data)){
		$data = http_build_query($data);
	}
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER, false);//不返回头部信息
	if(strtolower($method)!='get'){
		curl_setopt($ch, CURLOPT_POST, 1);
	}
	if($data!=null){
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);	
	}
	curl_setopt($ch,  CURLOPT_TIMEOUT,60);//超时设置
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  //结果是否显示出来,1不显示,0显示    
	//判断是否https
	if(strpos($url,'https://')!==false){
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		$UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
		curl_setopt($ch, CURLOPT_USERAGENT, $UserAgent);
	}
	$data = curl_exec($ch);
	curl_close($ch);
	if($data === FALSE) 
	{ 
	  $data = "curl Error:".curl_error($ch);
	} 
	return $data;

}

使用CURL发送json数据:

$url = 'https://example.com/api-endpoint';
$ch = curl_init($url);

$data = array(
    'key1' => 'value1',
    'key2' => 'value2'
);
$jsonData = json_encode($data);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch,  CURLOPT_TIMEOUT,60);//超时设置
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($jsonData)
));

// 禁用对证书的验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$response = curl_exec($ch);

if ($response === false) {
    echo 'cURL Error: ' . curl_error($ch);
}

curl_close($ch);


在请求头设置 Authorization 授权:

$url = 'http://example.com/api-endpoint';
$ch = curl_init($url);

$data = array(
    'key1' => 'value1',
    'key2' => 'value2'
);
$jsonData = json_encode($data);

$authorizationHeader = 'Bearer your_token_here';

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch,  CURLOPT_TIMEOUT,60);//超时设置
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($jsonData),
    'Authorization: ' . $authorizationHeader
));

$response = curl_exec($ch);

if ($response === false) {
    echo 'cURL Error: ' . curl_error($ch);
}

curl_close($ch);


使用CURL发送文件:

<?php
// 要发送的文件路径
$file_path = '/path/to/your/file.docx';

// 目标URL
$url = 'http://example.com/upload.php';

// 创建cURL资源
$curl = curl_init();

// 设置cURL选项
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// 构建POST数据
$post_data = array(
    'file' => new CurlFile($file_path, mime_content_type($file_path), basename($file_path))
);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

// 执行请求并获取响应
$response = curl_exec($curl);

// 检查是否有错误发生
if(curl_errno($curl)){
    $error_message = curl_error($curl);
    // 处理错误
    echo "cURL Error: " . $error_message;
}

// 关闭cURL资源
curl_close($curl);

// 处理响应
echo $response;
?>

在上面的示例中,您需要将$file_path设置为要发送的文件的实际路径,并将$url设置为目标URL。代码中的CurlFile类用于将文件包装成cURL可接受的形式,并使用mime_content_type()函数获取文件的MIME类型。



发表评论 (已有0条评论)

快来评论,快来抢沙发吧~