将命令行CURL命令转换为PHP脚本

将命令行CURL命令转换为PHP脚本,php,cookies,curl,Php,Cookies,Curl,我正在尝试将下面的curl命令转换为PHP脚本 curl -v -c cookies.txt -d "username=XXXX&password=XXXX&login=true&redirectMethod=POST&product=home.betfair.int&url=https://www.betfair.com/" https://identitysso.betfair.com/api/login >out.txt 2>&1

我正在尝试将下面的curl命令转换为PHP脚本

curl -v -c cookies.txt -d "username=XXXX&password=XXXX&login=true&redirectMethod=POST&product=home.betfair.int&url=https://www.betfair.com/" https://identitysso.betfair.com/api/login >out.txt 2>&1
当我在终端中执行该命令时,cookies.txt和out.txt文件都被正确创建。然而,当我尝试在PHP我不能得到它的工作

以下是我目前掌握的情况:

$fields = [
'username' => "lainga9",
'password' => "0a6c4822",
'login' => 'true',
'redirectMethod' => 'POST',
'product' => 'home.betfair.int',
'url' => 'https://www.betfair.com/'
];

$ch = curl_init();

curl_setopt_array($ch, [
CURLOPT_COOKIEJAR => 'cookies.txt',
CURLOPT_VERBOSE => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $fields,
CURLOPT_URL => 'https://identitysso.betfair.com/api/login',
CURLOPT_HEADER => 1
]);

$response = curl_exec($ch);

但是没有创建任何文件-有人有什么想法吗?

只需将响应放在所需文件中,与out.txt有关。但是对于cookiejar,cookies.txt文件可能是权限问题,apache无法写入该文件夹

您可以确保apache可以通过以下命令在适当的目录中写入:

sudo -u apache touch cookies.txt
下面是将响应写入文件时要添加到底部的内容:

file_put_contents('out.txt', $response);