2016-03-09 31 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); 
    } 
?> 
相关问题