我需要向url发出2个POST请求。 我可以通过POSTMAN成功完成这些操作,但似乎无法在PHP中正确实现。
我一直在寻找stackoverflow的答案和Google,并且发现了提示,我也直接从POSTMAN中拉出了php代码,以查看是否可以链接它们。我发现我需要做一个文本文件cookiejar,因为我需要保持相同的JSESSIONID,如果我没有那个JESSIONID则会出错。
<?php
function mycurl($url)
{
$ch = curl_init();
$cookie_file = 'C:\Pokemon\cookies.txt';
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$sub = substr($result, strpos($result, "Set") , strpos($result, ";") - strpos($result, "Set"));
echo $sub;
curl_setopt_array($ch, array(
CURLOPT_URL => "https://www.URL.com",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_COOKIEJAR => $cookie_file,
CURLOPT_COOKIEFILE => $cookie_file,
CURLOPT_POSTFIELDS => "id=A?pokemon=no?digimon=yes",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded"
) ,
));
$response2 = curl_exec($ch);
$err = curl_error($ch);
echo 'hi';
echo $response2;
if ($err)
{
echo "cURL Error #:" . $err;
}
else
{
echo $response2;
}
curl_close($ch);
}
mycurl("https://www.URL.com?id=A");
?>
我需要使用从第一个curl_exec获取的JESSESSIONID进行POST请求。
我不断收到此错误:
HTTP / 1.1 500 Internal Server Error Server:Apache-Coyote / 1.1 Content-Type:text / html; charset = utf-8 Content-Length:2508日期:Fri,08 Feb 2019 23:25:31 GMT X-连接:关闭 HTTP状态500-请求处理失败;嵌套的异常是java.lang.NullPointerException