多CURL请求中的随机500错误

时间:2013-04-12 10:32:27

标签: php jsonp curl-multi

我试图通过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'].')'; 
}?>

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

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;
}

修改已添加错误检查

1 个答案:

答案 0 :(得分:0)

  

为什么会一直这样?

您发送请求的服务器会实现此目的。只是请求可以失败。

处理它并设计失败。