将Linux Curl转换为PHP [英] Convert Linux Curl to PHP
问题描述
我整周都在尝试使用PHP curl启用与我的Web服务的连接,但是,我无法使其正常工作,所以我使用命令行尝试了curl,但令我惊讶的是。
I've been trying all week to try and enable connection to my web service using PHP curl, however, I couldn't make it work so I tried curl using command line and to my surprise.. it worked.
这是我在Linux curl上使用的命令:
Here's the command that I used using the linux curl:
curl -k -i -H内容类型:应用程序/ x-www-form-urlencoded -c cookies.txt -X POST https://< host> / appserver / j_spring_security_check -d j_username = admin& j_password = demoserver
如何将其转换为PHP代码?
How do you convert this to PHP code?
PS。我是新手,刚刚接触PHP不到一个月的时间,请原谅! :D
PS. I'm a newbie and have just been exposed to PHP with less than a month exp, forgive me! :D
推荐答案
您问以下linux终端 curl
命令如何关联到PHP curl的选项:
You asked how the following linux terminal curl
command relates to the options of PHP curl:
curl -k -i -H内容类型:application / x-www-form-urlencoded -c cookies.txt -X POST https://192.168.100.100:444/appserver/j_spring_security_check -d j_username = admin& j_password = demoserver
以下是上述选项/标志的列表:
Here is a list of the above options/flags:
- -k = CURLOPT_SSL_VERIFYPEER:false
- -i = CURLOPT_HEADER:true
- -H = CURLOPT_HTTPHEADER
- -c = CURLOPT_COOKIEJAR + CURLOPT_COOKIEFILE
- -X POST = CURLOPT_POST:是
- -d = CURLOPT_POSTFIELDS
- -k = CURLOPT_SSL_VERIFYPEER: false
- -i = CURLOPT_HEADER: true
- -H = CURLOPT_HTTPHEADER
- -c = CURLOPT_COOKIEJAR + CURLOPT_COOKIEFILE
- -X POST = CURLOPT_POST: true
- -d = CURLOPT_POSTFIELDS
这将导致以下结果:
<?php
$ch = curl_init();
$url = "https://192.168.100.100:444/appserver/j_spring_security_check";
$postData = 'j_username=admin&j_password=demoserver';
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1); // -X
curl_setopt($ch, CURLOPT_POSTFIELDS,$postData); // -d
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'application/x-www-form-urlencoded'
)); // -H
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); // -c
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); // -c
curl_setopt($ch, CURLOPT_HEADER, true); // -i
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // -k
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // see comment
echo curl_exec ($ch);
curl_close ($ch);
希望对您有所帮助。
这篇关于将Linux Curl转换为PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!