php - 如何正确发送 POSTFIELDS 到 cURL
问题描述
如图所示,我需要通过 cURL 发送 POST 数据。
我有这个代码
$data = [
'action' => 'order_cost',
'address' => 'http://91.211.117.3:720'
];
$query = http_build_query($data);
$url = "https://ap4.taxi/api/TaxiAPI.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvJFySHvqeKppEN9W',
)
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$output = curl_exec($ch);
curl_close($ch);
var_dump($output);
但我得到一个错误
我已经尝试了很多选择。邮递员正常发送POST,我收到答案。
请告诉我,我什至无法想象如何做到这一点。
解决方案
function execute_curl($url, $curlopt = array()){
$ch = curl_init();
$strCookie = session_name().'='.session_id().'; path=/';
session_write_close();
$default_curlopt = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_COOKIE => $strCookie,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 AlexaToolbar/alxf-1.54 Firefox/3.6.13 GTB7.1"
);
$curlopt = $curlopt + $default_curlopt;
curl_setopt_array($ch, $curlopt);
$response = curl_exec($ch);
$errormsg = curl_error($ch);
$errorCode = curl_errno($ch);
$results = array();
if($errormsg)
{
$results['status'] = 'error';
$results['data'] = $errormsg;
$results['errorcodetxt'] = curl_error_codes($errorCode);
}
else
{
$results['status'] = 'success';
$results['data'] = $response;
}
curl_close($ch);
return $results;
}
$curlopt = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => 1);
$curlresponse = execute_curl($url, $curlopt);
推荐阅读
- php - CodeIgniter 和 angularjs 中的上传文件错误
- java - 如何解决这个错误?!在 android studio 中遇到问题
- python - 通过 Python subprocess.Popen(或 Paramiko 会话)打开一个 PuTTY 窗口并在同一窗口中运行多个 shell 命令(如“top”)
- groovy - 在常规拆分集合中包含外来字符
- firebase - 如何通过 Firebase SDK 访问 Firestore 中的虚拟文档?
- android - 使用 RX Android 进行并行 API 调用
- python - 有没有办法从使用 sklearn 进行多元线性回归的预测中获取解释变量的值?
- android - 无法 repo init android 源代码镜像
- smalltalk - Smalltalk-80 字符含义/用法
- flutter - 如何按值更改应用栏中的图标?