【发布时间】:2017-01-27 00:45:41
【问题描述】:
我正在为客户评估 loader.io,但在让 APi 正常工作时遇到问题。我在 PHP 7 http://docs.loader.io/api/v2/post/tests.html#url-options
我被困在“创建测试”上。
文档说:
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..
我想通过 Symfony2 应用在 ajax 中执行此操作。
这是我得到的返回错误的内容:
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()?
【问题讨论】:
-
我应该指出,列出测试,运行测试......所有工作都通过 ajax 和上面的函数(我拿出一些 $post_data 在这里设置代码为简单起见)