doutaoer3148 2015-04-22 22:52
浏览 29
已采纳

PHP Curl没有发布数据,虽然它正在接收

I'm trying to make an API through PHP cURL and my client side (form submission) is receiving data from the API. it is just not posting it to the API.

 $ch = curl_init($url); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type= application/json"));
curl_setopt($ch, CURLOPT_POST, true);

// execute the request and store the return value.
$message = curl_exec($ch);

echo 'Message returned from API is:' .  $message; 

This is the code to receive the message from the API. It is returning a message so hopefully no errors there.

This is the api code (below):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>The API</title>
</head>

<body>
<?php

//date, job number, customer, worksite, duties performed, total hours spent, type of hours

$date = $_POST['date'];
$jobnumber = $_POST['jobnumber'];
$customer = $_POST['customer'];
$worksite = $_POST['worksite'];
$duties = $_POST['duties'];
$hours = $_POST['hours'];
$hourtype = $_POST['hourtype'];
$username = $_POST['username'];



$ok = true;
$error ;

//validate the inputs
if (empty($date)) {
    $ok = false;
    $error .= "Date field empty, ";
}

if (empty($jobnumber)) {
    $ok = false;
    $error .= "Job Number field empty, ";
}
if (empty($customer)) {
    $ok = false;
    $error .= "Customer field empty, ";
}

if (empty($worksite)) {
    $ok = false;
    $error .= "Worksite field empty, ";
}
if (empty($duties)) {
    $ok = false;
    $error .= "Duties field empty, ";
}
if (empty($hours)) {
    $ok = false;
    $error .= "Hours field empty, ";
}

if (empty($hourtype)) {
    $ok = false;
    $error .= "Hour type not specified, ";
}

$data = $date . ', ' . $jobnumber . ', ' . $customer . ', ' . $worksite . ', ' . $duties . ', ' . $hours . ', ' . $hourtype;
echo $data;
?>
</body>

</html>

My submit page is returning "Message returned from API is: , , , , , ,"

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • doyz51819 2015-04-22 23:45
    关注

    You seem to be passing in an array to cURL.

    $data = array( 'date' => $date, 'jobnumber' => $jobnumber, 'customer' => $customer, 'worksite' => $worksite, 'duties' => $duties, 'hours' => $hours, 'hourtype' => $hourtype, 'username' => $username );
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    

    Instead, build a query string for cURL to work with like this:

    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    

    The reason is because if you pass in an array, the Content-type header is automatically set to multipart/form-data, but you want application/x-www-form-urlencoded so you can access the POSTed information through the global $_POST.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路