使用libcurl将数据发布到PHP $ _Get

问题描述

数据未到达。 这是RaspBerryPI上的c程序:

#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
 
int main(void)
{
  CURL *curl;
  CURLcode res;
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl,CURLOPT_URL,"http://iautos.be/TestMake.PHP");  
    curl_easy_setopt(curl,CURLOPT_POSTFIELDS,"d=2020/09/16 16:00:00,21.6,P,");
      
    /* url Could be redirected,so we tell libcurl to follow redirection */ 
    curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1L);
 
    /* Perform the request,res will get the return code */ 
    res = curl_easy_perform(curl);
    /* Check for errors */ 
    if(res != CURLE_OK)
      fprintf(stderr,"curl_easy_perform() Failed: %s\n",curl_easy_strerror(res));
 
    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}

网络服务器上的目标PHP文件, 这在浏览器(Firefox)中效果很好: http://iautos.be/TestMake.php?d=2020/09/14%2004:00:00,

<?PHP
// Program to display URL of current page. 
// Put the host(domain name,ip) to the URL. 
$link = $_SERVER['HTTP_HOST']; 
// Append the requested resource location to the URL 
$link .= $_SERVER['REQUEST_URI']; 
echo 'Url: ',$link,"<br/>\n"; 
$Data = array();
$Data = explode(',',trim($_GET['d']) );
for ($x = 0; $x <= 2; $x++) {
      echo 'Data',$x,': ',$Data[$x],"<br/>\n" ;
}
print "Success.\n";
?>

问题: 当我使用Firefox发布数据时,效果很好。您可以对其进行测试(上面的链接)。 使用libcurl不会发送数据。 有什么问题吗?

这是RaspBerryPI上libcurl测试程序的输出

Url: iautos.be/TestMake.PHP<br/>
Data0: <br/>
Data1: <br/>
Data2: <br/>
Success.

这是Firefox的输出

Url: iautos.be/TestMake.PHP?d=2020/09/14%2004:00:00,Data0: 2020/09/14 04:00:00
Data1: 21.6
Data2: P
Success.

解决方法

CURL中的POSTFIELDS作为POST发送到服务器,因此这些参数将在$ _POST超全局数组中。

,

对不起,我是新手。 我做了更多测试。这有效:

@RestController
public class OrdersController {

@Autowired
private OrdersRepository ordersRepository;

@Autowired
private ManagersRepository managersRepository;

@Autowired
private DeliveryManRepository deliverymanRepo;

@Autowired
private ClientsRepository clientsRepository;

@Autowired 
private ProductsRepository productsRepos;



@PostMapping("orders/create/{idManager}/{idDeliveryMan}/{idClient}")
public ResponseEntity<?> createOrder(@Valid @RequestBody Orders order,@PathVariable String idManager,@PathVariable Long idClient,@PathVariable String idDeliveryMan){
    
    Managers manager= managersRepository.findByEmail(idManager);
    Clients client=clientsRepository.findByPhone(idClient);
    DeliveryMen deliveryMan = deliverymanRepo.findByEmail(idDeliveryMan);
    
    
    double price =0;
    for (Products p: order.getProducts_orders()) {
        price = price + p.getPriceProduct();
    }
    
    order.setIdClient(client.getPhone());
    order.setIdDeliveryMan(deliveryMan.getEmail());
    order.setIdManager(manager.getEmail());     
    order.setTotalPrice(price);     
    Orders newOrder = ordersRepository.save(order);
    
    return new ResponseEntity<Orders>(newOrder,HttpStatus.CREATED);
}
}

感谢您的帮助。