我使用curl通过API获取大量数据,这是代码:
function query($connectorGuid, $input, $userGuid, $apiKey, $additionalInput) {
$url = "https://api.import.io/store/connector/" . $connectorGuid . "/_query?_user=" . urlencode($userGuid) . "&_apikey=" . urlencode($apiKey);
$data = array("input" => $input);
if ($additionalInput) {
$data["additionalInput"] = $additionalInput;
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
// Query for tile Bundesliga Stats
$result = query("73df0478-4e6e-4f78-9ab5-f9866ac7a531", array(
"webpage/url" => "http://some_url.com/results_name/1234",
), $userGuid, $apiKey, false);
var_dump($result);
}
我发布了整个代码,以防需要,但我真正需要的是最后一部分:
$result = query("73df0478-4e6e-4f78-9ab5-f9866ac7a531", array(
"webpage/url" => "http:/some_url.com/result_name/1234",
), $userGuid, $apiKey, false);
有没有办法以某种方式将该URL设置为变量,然后将URL的“result_name”部分设置为另一个变量?
答案 0 :(得分:0)
你发布了这个:
$result = query("73df0478-4e6e-4f78-9ab5-f9866ac7a531", array(
"webpage/url" => "http://some_url.com/result_name/1234",
), $userGuid, $apiKey, false);
然后问这个:
有没有办法以某种方式将该URL设置为变量 然后设置" result_name"另一个变量的URL的一部分?
你有什么理由不能这样做:
$URL/$result_name/1234
所以你的代码应该是这样的:
$URL = "http://some_url.com";
$result_name = "result_name";
$result = query("73df0478-4e6e-4f78-9ab5-f9866ac7a531", array(
"webpage/url" => "$URL/$result_name/1234",
), $userGuid, $apiKey, false);
或许这可以让它更容易理解:
$URL = "http://some_url.com";
$result_name = "result_name";
$result = query("73df0478-4e6e-4f78-9ab5-f9866ac7a531", array(
"webpage/url" => $URL . "/" . $result_name . "/1234",
), $userGuid, $apiKey, false);
或者嘿,看看1234
。这些网址有很多部分。那么为什么不这样做呢:
// Create a URL array for all of the parts.
$URL_array = array();
$URL_array[] = "some_url.com";
$URL_array[] = "result_name";
$URL_array[] = "1234";
// Then set the result and use `implode` to create a coherent string from that URL array.
$result = query("73df0478-4e6e-4f78-9ab5-f9866ac7a531", array(
"webpage/url" => "http://" . implode("/", $URL_array),
), $userGuid, $apiKey, false);