2016-09-19 6 views
1

Я оцениваю loader.io для клиента, и у меня возникают проблемы с тем, чтобы APi работал правильно. Я на PHP 7 http://docs.loader.io/api/v2/post/tests.html#url-optionsLoader.io - PHP, curl and json

Я застрял в «создании теста».

документы говорят:

curl -X POST -H 'loaderio-auth: API_KEY' -H 'Content-Type: application/json' --data-binary '{"test_type":"cycling",    "total": 6000,    "duration":60,    "urls": [     {"url": "http://gonnacrushya.com"} ]}' https://api.loader.io/v2/tests 

Это здорово! Когда я добавляю свой APi-ключ и правильный URL-адрес, он работает нормально, тест создается.

Buuuut ..

Я хочу сделать это в AJAX через приложение Symfony2.

Вот что я получил, что это возвращение ошибки:

urls param has incorrect format 

function myAjaxCalledFunction() { 
    $test_data = '{"test_type":"cycling", "total": 10, "duration":5, "urls": [{"url": "http://www.my-configured-url.com"}] }'; 
    return $this->doCurlRequest('tests', 'POST', $test_data); 
} 


public function doCurlRequest($what_call, $request = 'GET', $post_data = null) 
{ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, 'https://api.loader.io/v2/' . $what_call); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('loaderio-auth: ' . $this->loader_api_key)); 

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $request); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POST,   1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS,  $post_data); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); 
    $response = curl_exec($ch); 
    return new Response($response); 
} 

находится там curl_setopt(), что я не хватает?

+0

Я должен отметить, что список тестов, запускать тесты ... все работают через AJAX и функция выше (я взял некоторые его $ post_data установлен код для простоты здесь) – Beertastic

ответ

1

Вы устанавливаете опцию CURLOPT_HTTPHEADER дважды. Вот почему он игнорирует первый. Вы можете нажать их обоих в массиве, как показано ниже, например:

curl_setopt($ch, CURLOPT_HTTPHEADER, 
    array('loaderio-auth: ' . $this->loader_api_key, 
      'Content-type: application/json'));