首页 > 解决方案 > CURL:主机名和端口后的路径

问题描述

我想在 PHP 中使用 curl将 JSON 数据发送到http://localhost:3000/certificates

这是我所做的:

$url = 'http://localhost';
$ch = curl_init($url);
$jsonData = array(
    'certno' => 'AB1234',
    'issueDate' => 'asdasd',
    'type' => 'asdasdasd'
);
$jsonDataEncoded = json_encode($jsonData);
curl_setopt($ch, CURLOPT_PORT, 3000); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
$result = curl_exec($ch);

如何添加 /certificates ?我不能简单地将它放在 url http://localhost/certificates中,对吗?

标签: phpcurlphp-curl

解决方案


推荐阅读