将php curl转换为curl

时间:2019-02-27 07:17:20

标签: php curl

我对PHP不熟悉,需要帮助将以下php代码转换为命令行curl。请指教。我使用邮递员尝试了不同的方式,但没有解决。

$username="";
$password="";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"https://www.xxx.co.uk/apinew");
curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('username' => $username,'pass' => $password,'type' => 'both','buildname' => 'Little Westwood House','subBname' => '','number' => '','depstreet' => '','street' => 'Bucks Hill','postcode' => 'WD4 9AR')));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

curl_close ($ch);

print_r($server_output);

1 个答案:

答案 0 :(得分:-1)

我建议您使用Guzzle HTTP客户端请求。从作曲家安装。使用命令行下载作曲家。这是链接https://getcomposer.org/

php composer.phar require guzzlehttp/guzzle

安装后,您需要使用Composer的自动加载器:

require 'vendor/autoload.php';

$client = new GuzzleHttp\Client();
$res = $client->request('POST', '"https://www.xxx.co.uk/apinew"', [
    'username' => $username,'pass' => $password,'type' => 'both','buildname' => 'Little Westwood House','subBname' => '','number' => '','depstreet' => '','street' => 'Bucks Hill','postcode' => 'WD4 9AR'
]);
echo $res->getStatusCode();
// "200"
echo $res->getBody();