繁体   English   中英

PHP curl响应字符串拆分

[英]PHP curl Response string splitting

这就是问题所在。 我正在从API中提取CSV文件,需要将其放入数组中。 这是我当前的代码:

$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);

问题是我得到响应:

string(245)“” Number“,” Name“,” Description“,” Type“,” Fixed Width Boolean“,”报价字符“,”分隔符“,”行尾“,” Header Boolean“,”列数“” 1“,”所有通话“,”所有通话数据“,”通话“,”假“,”无“,”,“,” \\ r \\ n“,” true“,” 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的第一行是标题,下面的数据是它需要对齐的数据。 另外,将来的请求将具有多行数据,并且它们也需要与标头匹配。 有任何想法吗?

如果您要处理CSV,请尝试使用内置功能 然后使用array_combine将标头粘贴为键:

$response = curl_exec($ch);
$csv_data = array_map('str_getcsv', explode("\n", $response));
$headers = array_shift($csv_data);
foreach ($csv_data as $v) {
    $data[] = array_combine($headers, $v);
}

举个例子:

$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;
$csv_data = array_map('str_getcsv', explode("\n", $response));
$headers = array_shift($csv_data);
foreach ($csv_data as $v) {
    $data[] = array_combine($headers, $v);
}
print_r($data);

输出:

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] => \r\n
            [Header Boolean] => true
            [Column Count] => 14
        )

)

您还可以将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
        )

)
*/

好吧,这有点“棘手”,但它可以工作。

PHP小提琴

 $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"
 )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM