0

Bootstrap Typeahead を入力するために、curl_multi を介して Web サービスから hal+json データを取得しようとしています。このコードを実行するたびに、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 に答える