sync.php在localhost中,数据通过它发送到服务器到sync1.php文件, 但是在执行localhost上的sync.php时,它只给出错误。 sync.php代码如下:
include_once('db/connection.php');
$len = $_REQUEST['key']; echo '<br>';
$sqlmob = "SELECT * FROM tblqeue limit 1";
$result = mysqli_query($conn, $sqlmob);
$res = mysqli_fetch_assoc($result);
$serialNo = $res['serialNo'];
$customerName = $res['customerName'];
$phoneNo = $res['phoneNo'];
$mail = $res['mail'];
$customerPincode = $res['customerPincode'];
$customerCity = $res['customerCity'];
$customerState = $res['customerState'];
$warrantyStatus = $res['warrantyStatus'];
$productDescription = $res['productDescription'];
$serviceType = $res['serviceType'];
$typeOfRepair = $res['typeOfRepair'];
$productFamily = $res['productFamily'];
$serv_charges = $res['serv_charges'];
$problemReported = $res['problemReported'];
$techComment = $res['techComment'];
$createdUser = $res['createdUser'];
$location = $res['location'];
$reloc = $res['rloc'];
//curl starts
$data = array(array (
'length' => $len,
'serialNo' => $serialNo,
'customerName' => $customerName,
'phoneNo' => $phoneNo,
'mail' => $mail,
'customerPincode' => $customerPincode,
'customerCity' => $customerCity,
'customerState' => $customerState,
'warrantyStatus' => $warrantyStatus,
'productDescription' => $productDescription,
'serviceType' => $serviceType,
'typeOfRepair' => $typeOfRepair,
'productFamily' => $productFamily,
'serv_charges' => $serv_charges,
'problemReported' => $problemReported,
'techComment' => $techComment,
'createdUser' => $createdUser,
'location' => $location,
'rloc' => $reloc
));
//print_r($data);echo '<br>';echo '<br>';
// json encode data
$data_string = json_encode($data);
//print_r($data_string);
// set up the curl resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.testsite.com/sync1.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
));
$output = curl_exec($ch);
$output = curl_exec($ch);
if ($output === false) {
$info = curl_getinfo($ch);
curl_close($ch);
die('error occured during curl exec. Additioanl info: ' .var_export($info));
} //every time its going to print the "error occured during curl exec.additional info
curl_close($ch);
$decoded = json_decode($output);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
var_export($decoded->response);
要接收sync1.php上的数据,我已编码,但数据即将到来,不知道原因:
$result_data = json_decode($output, TRUE);
$len = $result_data['length'];
$serialNo = $result_data['serialNo'];
$customerName = $result_data['customerName'];
$phoneNo = $result_data['phoneNo'];
$mail = $result_data['mail'];
$customerPincode = $result_data['customerPincode'];
$customerCity = $result_data['customerCity'];
$customerState = $result_data['customerState'];
$warrantyStatus = $result_data['warrantyStatus'];
$productDescription= $result_data['productDescription'];
$serviceType = $result_data['serviceType'];
$typeOfRepair = $result_data['typeOfRepair'];
$productFamily = $result_data['productFamily'];
$serv_charges = $result_data['serv_charges'];
$problemReported = $result_data['problemReported'];
$techComment = $result_data['techComment'];
$createdUser = $result_data['createdUser'];
$location = $result_data['location'];
$reloc = $result_data['rloc'];
如何发送和接收数据,任何人都可以提供帮助?
答案 0 :(得分:0)
错误输出($info
)显示许多零,您通常不希望看到它们,最明显的是http_code
。据推测,远程脚本确实会发回数据,但问题中没有详细说明,所以我认为没问题。
过去使用https
和curl
我总是发现使用ssl可用的一些选项有所帮助。我很好奇为什么数据必须是json_encoded但我希望以下方面有所帮助
<?php
include_once('db/connection.php');
$len = $_REQUEST['key'];
$sqlmob = "SELECT * FROM tblqeue limit 1";
$result = mysqli_query( $conn, $sqlmob );
$res = mysqli_fetch_assoc( $result );
if( $res ){
/* '$res' is already an array */
$data=$res;
$data['length']=$len;
$data_string = json_encode( $data );
/* download from curl.haxx.se and change path to suit */
$cacert='c:/wwwroot/cacert.pem';
$url='https://www.testsite.com/sync1.php';
$ch = curl_init();
/* set some ssl specific options */
if( parse_url( $url, PHP_URL_SCHEME )=='https' ){
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $ch, CURLOPT_CAINFO, realpath( $cacert ) );
}
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string );
curl_setopt( $ch, CURLOPT_HEADER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Length: ' . strlen( $data_string )
));
$output = curl_exec( $ch );
$info = curl_getinfo( $ch );
curl_close( $ch );
if ( $info['http_code']!=200 && $output === false ) {
die( 'error occurred during curl exec. Additional info: ' .var_export( $info ) );
}
$decoded = json_decode( $output );
if ( isset( $decoded->response->status ) && $decoded->response->status == 'ERROR' ) {
die( 'error occurred: ' . $decoded->response->errormessage );
}
var_export( $decoded->response );
}
?>