我不能使用PHP发布数据
我使用了这个代码,但我得到的结果总是空的,我不知道为什么我不能使用PHP发布数据,php,Php,我使用了这个代码,但我得到的结果总是空的,我不知道为什么 if (isset($_POST['uname'], $_POST['password'])){ $uname = test_input($_POST['uname']); $pw = test_input($_POST['password']); $data = array( "loginEmail" => $uname, "loginPassword
if (isset($_POST['uname'], $_POST['password'])){
$uname = test_input($_POST['uname']);
$pw = test_input($_POST['password']);
$data = array(
"loginEmail" => $uname,
"loginPassword" => $pw,
"clientURI" => "https://hk2.notify.windows.com/?token=AwYAAAC2bng45wHDpXWajWLTxGM1gxP1DcLlHTL8%2bhOfKVbkpYjl1U9tnUGrW4FSmwuiiWPKEyvSUoO5v9nfzFWZ097kdUeN8xDpAlp96Ilk3LCSN0sQNFuyU%3d"
);
$url_send ="https://dev-frontserver.com/api/login";
$str_data = json_encode($data);
$headers = array("Content-Type: application/json");
function sendPostData($url, $post, $headers){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
echo "result = " .sendPostData($url_send, $str_data, $headers);*/
}
知道我的代码有什么问题吗?我在中尝试了api,但在代码中无法使用
下面是从“curl_getinfo($ch);”返回的数据
您的修改代码和所做的更改是
请测试代码并告诉我是否需要任何修改我的猜测是因为至少有一个post变量未设置。在打开
后立即将错误报告添加到文件的顶部。也许可以检查一下:我建议查看cURL错误处理。我至少有18%肯定$str\u data=json\u encode($data)不应执行代码>。尝试注释掉那一行。尝试curl\u setopt($curlHandle,CURLOPT\u SSL\u VERIFYPEER,false);谢谢,我现在收到警告:http\u build\u query():参数1应为数组或object还有此错误通知:未定义索引:此行状态if($apiResponse['status']==1)
如果我对这些行进行注释,我会得到空的$resulta在注释发生错误的行之后,我现在从服务器得到响应,感谢以上代码是完成您的工作的唯一协议。。。操纵是需要从你身边。。。很高兴听到代码工作。
array (size=26)
'url' => string 'https://dev-fronapp.com/api/login' (length=47)
'content_type' => string 'application/json; charset=utf-8' (length=31)
'http_code' => int 200
'header_size' => int 268
'request_size' => int 384
'filetime' => int -1
'ssl_verify_result' => int 20
'redirect_count' => int 0
'total_time' => float 2.14
'namelookup_time' => float 0
'connect_time' => float 0.344
'pretransfer_time' => float 1.047
'size_upload' => float 253
'size_download' => float 3991
'speed_download' => float 1864
'speed_upload' => float 118
'download_content_length' => float 3991
'upload_content_length' => float 253
'starttransfer_time' => float 2.125
'redirect_time' => float 0
'redirect_url' => string '' (length=0)
'primary_ip' => string '' (length=13)
'certinfo' =>
array (size=0)
empty
'primary_port' => int 443
'local_ip' => string '' (length=12)
'local_port' => int 10484
<?php
if (isset($_POST['uname'], $_POST['password'])){
$uname = test_input($_POST['uname']);
$pw = test_input($_POST['password']);
$data = array(
"loginEmail" => $uname,
"loginPassword" => $pw,
"clientURI" => "https://hk2.notify.windows.com/?token=AwYAAAC2bng45wHDpXWajWLTxGM1gxP1DcLlHTL8%2bhOfKVbkpYjl1U9tnUGrW4FSmwuiiWPKEyvSUoO5v9nfzFWZ097kdUeN8xDpAlp96Ilk3LCSN0sQNFuyU%3d"
);
$url_send ="https://dev-frontserver.com/api/login";
$str_data = json_encode($data);
$headers = array("Content-Type: application/json");
echo "result = " .sendPostData($url_send, $str_data, $headers);
}
function sendPostData($url, $post, $headers){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($post));
//avoid checking ssl verifier
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
$result = curl_exec($ch);
if($result === false) {
throw new \RuntimeException(
'API call failed with cURL error: ' . curl_error($ch)
);
}
curl_close($ch);
$apiResponse = json_decode($result, true);
// Make sure we got back a well-formed JSON string and that there were no errors when decoding it
$jsonError = json_last_error();
if($jsonError !== JSON_ERROR_NONE) {
throw new \RuntimeException(
'API response not well-formed (json error code: ' . $jsonError . ')'
);
}
// Print out the response details
if($apiResponse['status'] === 1)//check field where
{
// No errors encountered
return $result;
}
else {
// An error occurred
die("Error:");//show error message from api
}
}
?>