2016-03-09 58 views
1

sync.php位於本地主機,通過該主機將數據發送到服務器以將文件sync1.php, 但在執行本地主機上的sync.php時,僅給出錯誤。該sync.php代碼如下:cURL在從本地主機向活動服務器發送數據時發生錯誤

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

說明什麼錯誤,請,因爲'「只有它給錯誤」'是不是很描述 – RamRaider

+0

在執行sync.php這是發送數據到sync1.php其給予錯誤「在curl執行期間發生錯誤Additioanl info:」,$ output === false這就是爲什麼上述錯誤即將到來,爲什麼它給$輸出false沒有得到完全。 – Ashish

+0

這基本上就是你在代碼中所擁有的 - 錯誤輸出包含什麼內容? – RamRaider

回答

0

錯誤輸出($info)顯示了很多通常不會看到它們的零,最值得注意的是http_code。據推測,遠程腳本確實發回數據,但在問題中沒有詳細說明,所以我認爲沒關係。

在過去我使用httpscurl我一直髮現使用一些可用於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); 
    } 
?> 
相關問題