【发布时间】:2017-01-24 20:34:30
【问题描述】:
我需要把这个 cURL 命令翻译成 PHP cURL 代码:
curl -v -b session_cookies.txt -L -H "Content-Type: application/x-www-form-urlencoded" -v -d 'j_username=manager-sitelogin@gmail.com&j_password=site1' "https://login.uat.site.be/openid/login.do"
我试过这个,但它似乎不起作用,结果我一直空着:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://login.uat.site.be/openid/login.do");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "j_username=manager-sitelogin@gmail.com&j_password=site1");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_COOKIEJAR, public_path().'/session_cookies.txt');
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
dd($result); 我一直收到空字符串,但它在标准 curl 命令中有效。
【问题讨论】:
-
仅用于调试,添加
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);。 -
login.uat.site.be是真实地址吗?它对我说“* 无法解析主机:login.uat.site.be”。 -
@axiac :请假设它是。我真的不能把我的客户 URL 扔在这里。对此我感到非常抱歉。
-
你为什么不使用 guzzle 库,它是 curl 的一个包装器,它可以让你在几行代码中做到这一点?