使用curl获取字符串并从字符串中查找特定值

时间:2014-01-10 19:48:19

标签: php if-statement curl

在此代码中,$new_el[0]的值应该与我们在服务器响应中获得的值相同。我希望代码能够运行,如果来自服务器的值是$new_el[0]=0;那么$new_el[0]="CJ";的值以及来自服务器的值是$new_el[0]=1;那么值是$new_el[0]=01;基本上,0在每个数字之前添加,例如01,02,03($new_el[0] = 0除外)。

  function curlUsingGet($url, $data)
   {

if(empty($url) OR empty($data))
{
    return 'Error: invalid Url or Data';
}

$fields_string = '';
foreach($data as $key=>$value) { $fields_string .= $key.'='.urlencode($value).'&'; }
rtrim($fields_string,'&');

$urlStringData = $url.'?'.$fields_string;
$_SESSION['urlStringData']=$urlStringData;
 $cookie= "cookie.txt";
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,0);
curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt($ch, CURLOPT_COOKIESESSION,true);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_FRESH_CONNECT,true);
curl_setopt($ch, CURLOPT_URL,$urlStringData);

$return = curl_exec($ch);

$arrel = explode('|',$return);

foreach($arrel as $val)
{


$new_el=explode('~',$val);


  echo $new_el[0];
  echo $new_el[1];

}

curl_close($ch);    
}

$data = array('u' =>$_SESSION['councel']);
echo curlUsingGet('http://"url":8080/causelist/CauseListData',$data);

1 个答案:

答案 0 :(得分:0)

您可以使用sprintf将数字填入0。尝试以此为出发点。

$new_el[0] = ($new_el[0] == 0) ? 'CJ' : sprintf("%02d", $new_el[0]);