2015-12-03 97 views
1

所以,這是問題。我從一個API拉取一個CSV文件,並且需要把它放到一個數組中。這裏是我當前的代碼:PHP curl響應字符串拆分

$url = "https://www.*****************"; 
$myvars = 'username=*********&password=*************'; 

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text')); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

if(!curl_exec($ch)){ 
    die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch)); 
} else { 
    $response = curl_exec($ch); 
    $exploded = nl2br($response); 
    //echo $response."<br>"; 
    var_dump($exploded); 
} 
curl_close($ch); 

的問題是我得到的迴應:

串(245) 「」 號碼 「 」姓名「, 」說明「, 」類型「,」固定寬度布爾「,」引用字符「,」分隔符「,」行結束順序「,」標題布爾值「,」列計數「 」1「,」全部呼叫「,」全部呼叫數據「 , 「假」, 「無」, 「」, 「\ r \ n」, 「真」, 「14」 「

這是CSV兩條線,但出來在一個單一的字符串行中。我嘗試過爆炸它,但它似乎有兩個分隔符,我試圖分裂它,但它不會找到第二個分隔符。我希望它產生這樣的:

array(
"Number" => 1, 
"Name" => "All Calls", 
"Description" => "All Call Data", 
"Type" => "Call", 
"Fixed Width Boolean" => false, 
"Quote Character" => "None", 
"Delimiter Character" => ",", 
"End of Line Sequence" => "\r\n", 
"Header Boolean" => true, 
"Column Count" => 14 
); 

的CSV的第一行是頭和數據下面是它需要對齊到的數據。未來的請求也會有多行數據,並且它們也需要與標題匹配。有任何想法嗎?

+0

所以你試過爆炸這樣嗎? '$ exploded = explode(「,」,$ response);' – VIDesignz

+0

您應該使用代碼格式顯示文件的內容。另外,不需要顯示'var_dump()'的輸出,只顯示文件本身的幾行內容。 – miken32

+0

與您的問題無關,但您正在使用兩次'curl_exec'調用執行http請求兩次。 –

回答

1

你也可以把你的csv字符串變成一個文件指針,並使用fgetcsv就可以了。這裏是它如何工作的一個例子:使用

Josh:~$ php -a 
Interactive shell 

php > $data = <<<CSV 
<<< > "col1","col2" 
<<< > "d1","," 
<<< > CSV; 
php > echo $data; 
"col1","col2" 
"d1","," 
php > $fp = fopen('data://text/plain,' . $data, 'r'); 
php > while (($row = fgetcsv($fp)) !== false) { 
php { var_dump($row); 
php { } 
array(2) { 
    [0]=> 
    string(4) "col1" 
    [1]=> 
    string(4) "col2" 
} 
array(2) { 
    [0]=> 
    string(2) "d1" 
    [1]=> 
    string(1) "," 
} 

你們的榜樣這將是類似於以下

$response = <<<CSV 
"Number","Name","Description","Type","Fixed Width Boolean","Quote Character","Delimiter Character","End of Line Sequence","Header Boolean","Column Count" 
"1","All Calls","All Call Data","Call","false","None",",","\r\n","true","14" 
CSV; 
$fp = fopen('data://text/plain,' . $response, 'r'); 
$data = []; 
$header = fgetcsv($fp); // first row is column headers 
while (($row = fgetcsv($fp)) !== false) { 
    $data[] = array_combine($header, $row); 
} 
print_r($data); // list of rows with keys set to column names from $header 
/* 
Array 
(
    [0] => Array 
     (
      [Number] => 1 
      [Name] => All Calls 
      [Description] => All Call Data 
      [Type] => Call 
      [Fixed Width Boolean] => false 
      [Quote Character] => None 
      [Delimiter Character] => , 
      [End of Line Sequence] => 

      [Header Boolean] => true 
      [Column Count] => 14 
     ) 

) 
*/ 
+0

我以爲我有答案,但你的是完美的。萬分感謝。我一直堅持這3天。 – griff4594

+0

不客氣。當我厭倦了無法自己解析它們時,我一直在使用'data://'+'fgetcsv'技巧。 –

+0

我可以爲該問題獲得+1嗎?大聲笑?這是一次學習體驗。我一直在編碼很長一段時間,但我討厭做捲曲請求,因爲響應數據總是非常不一致。 – griff4594

-1
"Delimiter Character" => ",", 

這會給你一個問題,因爲PHP會爆炸也逗號

+0

的確如此。經過測試,它打破了陣列的完整性。 – VIDesignz

+0

這是一條評論,而不是答案。 –

+0

你是對的。當我做到這時確實會造成問題。這是問題疊加的地方。 – griff4594

2

如果你處理的CSV,請嘗試使用built-in function這樣的:

$response = curl_exec($ch); 
$csv_array = str_getcsv($response); 
print_r($csv_array); 
+0

如果您按照評論中的要求編輯您的問題以包含文件中的10行,那麼您將得到更好的建議。 – miken32

-1

那麼,這是有點 「哈克」,但工程....

PHP Fiddle

$response = '"Number","Name","Description","Type","Fixed Width Boolean","Quote Character","Delimiter Character","End of Line Sequence","Header Boolean","Column Count","1","All Calls","All Call Data","Call","false","None",",","\r\n","true","14"'; 

$response = preg_replace('/[,]/', "*", $response); 
$response = str_replace('*"*"*', '*","*', $response); 

$exploded = explode("*", $response); 

$count = count($exploded)/2; 

$newArray = []; 

for($i=0; $i<$count; ++$i){ 
    $newArray[$exploded[$i]] = $exploded[$i+$count]; 
} 

print_r($newArray); 

它打印

Array 
(
    ["Number"] => "1" 
    ["Name"] => "All Calls" 
    ["Description"] => "All Call Data" 
    ["Type"] => "Call" 
    ["Fixed Width Boolean"] => "false" 
    ["Quote Character"] => "None" 
    ["Delimiter Character"] => "," 
    ["End of Line Sequence"] => "\r\n" 
    ["Header Boolean"] => "true" 
    ["Column Count"] => "14" 
) 
+0

不要這樣解析csv。根據需要使用內置的'fgetcsv'或'str_getcsv'。 –

+0

@JoshJ不投下一個可行的答案...我可以解析一個csv,我真的很想。我甚至說這有點「哈克」...... geeez。 – VIDesignz

+0

這就是論壇應該工作的方式。投票最佳做法,投下不良想法。與個人無關。我相信你知道你的東西,並在這裏提供很多幫助。 –

-1

我試着做下面,這裏是結果:

$url = "https://www.*****************"; 
$myvars = 'username=*********&password=*************'; 

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

if(!$response = curl_exec($ch)){ 
    die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch)); 
} else { 
    $strget = str_getcsv($response);  
    var_dump($strget); 
} 
curl_close($ch); 

結果回來的數組:

Array ( 
    [0] => Number 
    [1] => Name 
    [2] => Description 
    [3] => Type 
    [4] => Fixed Width Boolean 
    [5] => Quote Character 
    [6] => Delimiter Character 
    [7] => End of Line Sequence 
    [8] => Header Boolean 
    [9] => Column Count "1" 
    [10] => All Calls 
    [11] => All Call Data 
    [12] => Call 
    [13] => false 
    [14] => None 
    [15] => , 
    [16] => \r\n 
    [17] => true 
    [18] => 14 
) 

的問題是應該有可以是2個數組,一個嵌套在另一箇中,或者設置在上面原始的期望解決方案中。主要問題是:

[9] => Column Count "1" 

就在這裏,其他數組或換行符應該在哪裏。我給你一個+1 miken32的典型正確答案。

修訂

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_VERBOSE, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

if(!$response = curl_exec($ch)){ 
    die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch)); 
} else { 
    $strget = str_getcsv($response,"\n"); 
    //echo $strget."<br>"; 
    print_r($strget); 
} 
curl_close($ch); 

此輸出以下:

Array ( 
    [0] => Number,"Name","Description","Type","Fixed Width Boolean","Quote Character","Delimiter Character","End of Line Sequence","Header Boolean","Column Count" 
    [1] => 1,"All Calls","All Call Data","Call","false","None",",","\r\n","true","14" 
) 

所以str_getcsv($response,"\n");是解決方案的一部分。還有誰?

+0

它看起來像你的輸入csv數據格式不正確。它應該在第10列之後有一個換行符,但不是。 –

+0

我嘗試使用分隔符「/ n」與str_getcsv,它確實分開數組。但是現在我有很奇怪的包裝。 – griff4594