2017-09-18 2 views
0

같은 PHP 페이지에서 두 번의 별도 cURL 요청을하고 있습니다. 페이지 자체를로드하면 각 요청이 다른 올바른 데이터를 리턴하면서 예상대로 작동합니다.AJAX를 통해로드 할 때 동일한 데이터를 보여주는 두 개의 cURL 요청

그러나 AJAX를 통해 페이지를로드하면 두 번째 요청은 첫 번째 요청과 동일한 데이터를 표시합니다. 왜 이런 일이 일어나는 걸까요? 아래 코드는 다음과 같습니다 AJAX를 통해로드 할 때 그들은 완전히 다른 엔드 포인트에서 결과를 반환해야 할 때

$auth = base64_encode('user:'.$api_key); 
    $data = array(
     'apikey'  => $api_key, 
    ); 
    $json_data = json_encode($data); 
    $ch = curl_init(); 
    $ch2 = curl_init(); 

    $curlopt_url = "https://us7.api.mailchimp.com/3.0/reports/".$_GET['id']; 
    curl_setopt($ch, CURLOPT_URL, $curlopt_url); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Basic '.$auth)); 
    curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0'); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); 
    $result = curl_exec($ch); 
    $results = json_decode($result, true); ?> 

    $curlopt_url_b = "https://us7.api.mailchimp.com/3.0/reports/".$_GET['id'].'/sent-to/?count=5000 '; 

    curl_setopt($ch2, CURLOPT_URL, $curlopt_url_b); 
    curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "GET"); 
    curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Basic '.$auth)); 
    curl_setopt($ch2, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0'); 
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch2, CURLOPT_TIMEOUT, 10); 
    curl_setopt($ch2, CURLOPT_POST, true); 
    curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch2, CURLOPT_POSTFIELDS, $json_data); 
    $recipient_result = curl_exec($ch2); 
    $recipients = json_decode($recipient_result, true); 

는 수신자 == $ 결과를 $. 뭐라 구요?

+0

결과와 json_encode()를 한 번 결합해야합니다. '$ results'와'$ recipients'로 무엇을하고 있습니까 –

+0

@LawrenceCherone하지만 왜 AJAX가 아닌 자체적으로로드 할 때 작동합니까? – BFWebAdmin

+0

'{} {}'을 멋지게 본다면 2 개의 json 객체를 얻지 못할 가능성이 높습니다. jquery는 단지 하나만 파싱합니다. –

답변

0

문제는 cURL 요청이 아니라 AJAX 요청 때문입니다. AJAX 요청은 URL에 추가 매개 변수 (이 경우 'ajax = true')를 추가했습니다.이 매개 변수는 $ _GET 변수로 전달되어 잘못된 끝점을 요청합니다. URL이 JS에 의해 변경되지 않았기 때문에 독립적으로로드 될 때 올바르게로드되었습니다.