0

我正在尝试通过 curl_multi 从 Web 服务获取一些 hal+json 数据以填写 Bootstrap Typeahead。每次运行这段代码,我curl_multi里面的请求有的会是500,有的会返回我需要的数据。500 是完全随机的;下次我加载页面时,不同的查询将改为 500(而且我没有收到错误)。为什么这种情况不断发生?

<?php
$curlurl = 'https://service.domain.com/'.$_POST['customer-name'].'/contact?callback=?';
$cuurl = singleRequest($curlurl);
global $contacts_typeahead_data;
$contacts_typeahead_data = array();
$clinks = $cuurl['_links']['https://service.domain.com/rel/contacts'];
for ($i=0; $i < count($clinks); $i++) { 
         $data[] = 'https://service.domain.com'.$clinks[$i]['href'].'?callback=?';
};
print_r($data);
$datar = multiRequest($data);
print_r($datar);    
foreach($datar as $c){ 
        $cs = json_decode($c, true);?>

        <option value="<?php echo $cs['id']; ?>"><?php echo $cs['id'].' ('.$cs['info'][0]['name'].')'; ?></option>
        <?php $contacts_typeahead_data[] = $cs['id'].' ('.$cs['info'][0]['name'].')'; 
}?>

这是基于http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/的 singleRequest 和 multiRequest 的代码:

function multiRequest($data, $options = array()) {

// array of curl handles
$curly = array();
// data to be returned
$result = array();

// multi handle
$mh = curl_multi_init();

// loop through $data and create curl handles
// then add them to the multi-handle
foreach ($data as $id => $d) {

      $curly[$id] = curl_init();

      $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
      $username = base64_encode('username');
      $password2 = 'password';
      $auth_token = $username . $password2 . 'QQ==';
      curl_setopt($curly[$id], CURLOPT_URL,            $url);
      curl_setopt($curly[$id], CURLOPT_HEADER,         0);
      curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($curly[$id], CURLOPT_USERPWD, "$username:$password2");
      curl_setopt($curly[$id], CURLOPT_SSLVERSION,3);
      curl_setopt($curly[$id], CURLOPT_HTTPHEADER, array(   
                    'Accept: application/hal+json',
                    'Access-Control-Allow-Origin: *',                                                                       
                    'Content-Type: application/hal+json',
                    'Authorization: Basic ' . $auth_token
      ));
      curl_setopt($curly[$id], CURLOPT_HTTPAUTH, CURLAUTH_ANY);
      curl_setopt($curly[$id], CURLOPT_CONNECTTIMEOUT, 0);
      curl_setopt($curly[$id], CURLOPT_TIMEOUT, 30);
      curl_setopt($curly[$id], CURLOPT_PORT, 443);
      curl_setopt($curly[$id], CURLOPT_REFERER, $_SERVER['HTTP_REFERER']);
      curl_setopt($curly[$id], CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
      curl_setopt($curly[$id], CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($curly[$id], CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($curly[$id], CURLOPT_COOKIE, 'cert_url=https://service.domain.com/cert/yagowyefayygwflagliwygelifyaigwepifgpai');

      // post?
      if (is_array($d)) {
            if (!empty($d['post'])) {
                   curl_setopt($curly[$id], CURLOPT_POST,       1);
                   curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
            }
      }

      // extra options?
      if (!empty($options)) {
           curl_setopt_array($curly[$id], $options);
      }

   curl_multi_add_handle($mh, $curly[$id]);
 }

 // execute the handles
 $running = null;
 do {
      curl_multi_exec($mh, $running);
 } while($running > 0);


 // get content and remove handles
 foreach($curly as $id => $c) {
        // cURL error number
        $curl_errno = curl_errno($c);

        // cURL error message
        $curl_error = curl_error($c);

        // output if there was an error
        if ($curl_error) {
            echo "    *** cURL error: (".$curl_errno.") ".$curl_error;
        } else {
              $result[$id] = curl_multi_getcontent($c);  
        }

        curl_multi_remove_handle($mh, $c);
}

 // all done
 curl_multi_close($mh);

 return $result;
}

function singleRequest($curlurl){
    $username = base64_encode('username');
    $password2 = 'password';
    $auth_token = $username . $password2 . 'QQ==';
    curl_setopt($ch, CURLOPT_URL,            $url);
    curl_setopt($ch, CURLOPT_HEADER,         0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password2");
    curl_setopt($ch, CURLOPT_SSLVERSION,3);
    curl_setopt($ch, CURLOPT_HEADER, false);     
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(   
            'Accept: application/hal+json',
            'Access-Control-Allow-Origin: *',                                                                       
            'Content-Type: application/hal+json',  
            'Authorization: Basic ' . $auth_token                                                  
    ));  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);     
    curl_setopt($ch, CURLOPT_COOKIE, 'cert_url=https://service.domain.com/cert/yagowyefayygwflagliwygelifyaigwepifgpai');
            $result = curl_exec($ch);  
            if($result === false){
                  echo 'Curl error: ' . curl_error($ch). '<br><br>';
                  print_r(error_get_last());
            }
    $response = json_decode($result, true); 
    curl_close($ch);

    return $response;
}

编辑添加错误检查

4

1 回答 1

0

为什么这种情况不断发生?

您向其发送请求的服务器会执行此操作。只是请求可能会失败。

处理它并设计失败。

于 2013-04-12T10:33:35.827 回答