我是使用curl和json的新手。我得到$ courier,$ ship_id和$ rate的正确值。 但$ obj1中没有值。我检查了我的链接,它返回的json很好。 我不知道这里有什么问题。它可能是卷曲不能超过一次/两次的东西吗? 请帮忙。提前谢谢。
<?php
$a = 1;
if( $a == '1' ) {
$b = 'www.somelink.com?ship_id='.$ship_id.'&service_type='.$currier_name.'&rates='.$rate;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $b);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result, true);
print_r($obj);
$courier = $obj['ship']['0']['Service'];
$ship_id = $obj['ship_id'];
//echo $courier;
$rate = $obj['ship']['0']['Rate'];
$currier_name = str_replace(' ', '%20', $courier);
$a = 'www.xyz.com';
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_URL, $a);
$result1 = curl_exec($ch1);
curl_close($ch1);
$obj1 = json_decode($result1, true);
if(empty($obj1))
{
$courier = ""; $ship_id="" ; $rate="";
$courier = $obj['ship']['1']['Service'];
$ship_id = $obj['ship_id'];
//echo $courier;
$rate = $obj['ship']['1']['Rate'];
$currier_name = str_replace(' ', '%20', $courier);
$a2 = 'www.xyz.com?ship_id='.$ship_id.'&service_type='.$currier_name.'&rates='.$rate;
echo $a2;
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_URL, $a2);
$result2 = curl_exec($ch2);
curl_close($ch2);
$obj1 = json_decode($result2, true);
print_r($obj1);
}
}
&GT;
<td>
<input class="carrier input-text" value="<?php echo $courier; ?>" type="text" name="carrier" id="carrier" style="width:100%;"/>
</td>
<td>
<input class="required-entry input-text" value="<?php echo $obj1['tracking_id']; ?>" type="text" id="tracking_id" name="tracking_id" style="width:100%;"/>
</td>
<td>
<input class="required-entry input-text" value="<?php echo $obj1['path']; ?>" type="text" id="label_path" name="label_path" style=""/>
</td>