使用curl获取成功消息后更新数据库中的值

时间:2018-11-25 06:34:24

标签: php curl

使用curl传递url中的值后,我得到以下消息:

{"AddManifestDetails":[{"AuthKey":"Valid","ReturnMessage":"successful",}]

如果 ReturnMessage 成功,而不是我想要更新数据库中的值,则尝试使用以下代码:

<?php

$data = 
array (
'OrderNo' => $order_id,
'AirWayBillNO' => $resultc[0]['awb'],
);

$url = "http://114.143.206.69:803/StandardForwardStagingService.svc/AddManifestDetails";

$data = json_encode($data);
$curl = curl_init($url);

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

$curl_response = curl_exec($curl);
curl_close($curl);
echo $curl_response ."\n";
$res=json_decode($curl_response);

foreach ($res->curl_response as $values) 
{
if($values->ReturnMessage=='successful')
    {
      $usql="update  do_order set tracking_id='".$resultc[0]['awb']."',shipping_name='xpress', where order_id='".$order_id."'";       
      $result=$db_handle->executeUpdate($usql);  
      echo "1";die;
    }
    else
    {
      echo $values->ReturnMessage;die;
    }
}

这里是完整代码:https://pastebin.com/EvcEY0xp

结果

注意:未定义的属性:stdClass :: $ curl_response

警告:为foreach()提供了无效的参数

1 个答案:

答案 0 :(得分:1)

$res将已经包含来自curl请求的解码响应,该请求仅具有1个属性-AddManifestDetails

尝试以下操作:

<?php

$data = 
array (
'OrderNo' => $order_id,
'AirWayBillNO' => $resultc[0]['awb'],
);

$url = "http://114.143.206.69:803/StandardForwardStagingService.svc/AddManifestDetails";

$data = json_encode($data);
$curl = curl_init($url);

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

$curl_response = curl_exec($curl);
curl_close($curl);
echo $curl_response ."\n";
$res=json_decode($curl_response);

if($res->AddManifestDetails[0]->ReturnMessage=='successful')
{
    $usql="update  do_order set tracking_id='".$resultc[0]['awb']."',shipping_name='xpress' where order_id='".$order_id."'";       
    $result=$db_handle->executeUpdate($usql);  
    echo "1";
    die;
}
else
{
    echo $res->AddManifestDetails[0]->ReturnMessage;
    die;
}