php curl自定义函数获取POST接口信息
php curl自定义函数获取POST接口信息<?php
function curlRequest($url, $postData = [])
{
// 初始化cURL会话
$ch = curl_init();
// 将表单数据转换为URL编码的查询字符串
$cookie = '';
// 设置cURL选项
$postDataString = http_build_query($postData);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 设置请求头,如果需要动态设置Content-Length,可以计算$postDataString的长度
$headers = [
'Accept: application/json, text/plain, */*',
'Accept-Encoding: gzip, deflate, br',
'Accept-Language: zh-CN,zh;q=0.9',
'Content-Length: 0',
];
if (!empty($cookie)) {
$headers[] = 'Cookie: ' . $cookie;
}
//var_dump($headers);exit;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'spcookie.txt'); // 存储Cookie的文件
curl_setopt($ch, CURLOPT_COOKIEFILE, 'spcookie.txt'); // 发送时读取Cookie的文件
// 执行cURL请求并获取响应
$response = curl_exec($ch);
// 检查cURL错误
if ($response === false) {
$error = curl_error($ch);
echo "cURL Error: {$error}";
curl_close($ch);
return null;
}
// 处理响应数据
$data = json_decode($response, true);
// 关闭cURL会话
curl_close($ch);
// 返回处理后的数据或原始响应(根据需要)
return $data;
}
// 要发送请求的URL
$url = '';
// 准备要发送的表单数据,作为关联数组
$postData = [
'page' => 1, // 请求的页码,此处为第一页
'rows' => 18, // 每页欲显示的记录条数,这里设置为18条
];
$responseData = curlRequest($url, $postData);
// 现在你可以使用$responseData来处理返回的数据了
if ($responseData !== null) {
$rows = $responseData["rows"];
}
$i = 0;
foreach ($rows as $row) {
$goodsid = $row['goodId'];
$url = '$goodsid; //获取多图片
$postData = [
'goodsId' => $goodsid, //商品id goodsId=249372724
];
$goodsMediasData = curlRequest($url, $postData);
echo $i++ . ' 商品id: ' . $row['goodId'] . ' 商品名称:' . $row['name'] . ' 商品货号:' . $row['goodsSn'] . ' 商品采购价:' . $row['factoryPrice'] ;
// 假设 $goodsMediasData 已经包含了你从某处获取的图片数据
if ($goodsMediasData !== null && isset($goodsMediasData["rows"])) {
$Medrows = $goodsMediasData["rows"];
// 指定下载图片的本地目录
$downloadDir = '';
// 确保下载目录存在且可写
if (!is_dir($downloadDir) || !is_writable($downloadDir)) {
die('Download directory does not exist or is not writable.');
}
foreach ($Medrows as $Medrow) {
// 获取图片 URL
$imgUrl = $Medrow["imgUrl"];
// 从 URL 获取图片数据
$imgData = file_get_contents($imgUrl);
// 检查是否成功获取图片数据
if ($imgData === FALSE) {
echo "Failed to download image from URL: " . $imgUrl . "\n";
continue;
}
// 生成本地文件名(可以根据需要自定义)
$filename = basename($imgUrl);
$localFilePath = $downloadDir . $filename;
$imageUrl = '' . $filename;
// 将图片数据写入本地文件
//$result = file_put_contents($localFilePath, $imgData);
// 检查是否成功写入文件
// if ($result === FALSE) {
// echo "Failed to save image to file: " . $localFilePath . "\n";
// } else {
echo '商品图片:<img src="' . $imageUrl . '" width="100" height="100">';
// }
}
} else {
echo "No image data to download.\n";
}
$goodsSn =$row['goodsSn'];
$goodsSn = urlencode($goodsSn);
$url = '$goodsSn; //获取库存
$postData = [
'goodsSn' => $goodsSn,
];
$goodsSnData = curlRequest($url, $postData);
if ($goodsSnData !== null && isset($goodsSnData["rows"])) {
$Snrows = $goodsSnData["rows"]["number"];
echo urldecode($goodsSn).'库存:'.$Snrows.'件' ;
}
echo '<br>';
}
?>
页:
[1]