创建一个php和curl回调函数

时间:2017-02-20 12:28:11

标签: php web-services curl

我正在尝试从服务器获取发送时的响应,并更新我的数据库。到目前为止,我所拥有的代码只有在通过浏览器运行时才会得到响应,但它没有从服务器获得任何进一步的响应。到目前为止,这是我的代码:

if($ch !== false){
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    //Attach our encoded JSON string to the POST fields.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //Set the content type to application/json
    // curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json;odata=verbose',
        'Content-length:' . strlen($jsonDataEncoded))
    );
}

//Execute the request
$result = curl_exec($ch);

if ($result === false){
    curl_close($ch);
    echo "Error".curl_errno($ch). "\n";
}
else {
    curl_close($ch);
    $decoded = json_decode($result, true);

    $data['unique_id'] = $decoded["clientCorrelator"];
    $data['subscriber_number'] = $decoded["endUserId"];
    $data['payment_status'] = $decoded["transactionOperationStatus"];
    $data['payment_gross'] = $decoded["paymentAmount"]["totalAmountCharged"];
    $data['txn_id'] = $decoded["ecocashReference"];

    $this->payments->insertTransaction($data);
    die;
}

0 个答案:

没有答案