Я заметил пару ошибок в коде. Во-первых, для решения вашего основного вопроса вы не совсем правы с вашим запросом cURL
. Для того, чтобы отправить запрос с помощью POST
cURL
и получить ответ, используйте следующий код:
$ch = curl_init($url); // Set cURL url
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // Send request via POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // Set POST data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // curl_exec() returns response
curl_setopt($ch, CURLOPT_HEADER, "Content-Type: application/x-www-form-urlencoded");
$response = curl_exec($ch); // Return the request response
curl_close($ch);
Кроме того, в вашем коде, вы используете $encoded = substr($encoded, 0, strlen($encoded)-1);
. Это необязательно, вместо этого просто сделайте $encoded = substr($encoded, 0, -1);
.
В-третьих, ваше регулярное выражение в настоящее время недействительно. Вы должны добавить /
в начало и конец строки: preg_match('/!\d+!/', $encoded, $zip);
Наконец, ваши foreach
петли не полностью необходимо. Вместо этого вы можете использовать функцию http_build_query
: $encoded = http_build_query($_GET) . "&" . http_build_query($_POST);
. Это также делает вашу линию substr
бессмысленной.
Поэтому вы хотите что-то вроде:
$encoded = "";
// Check to make sure the variables encoded are actually set
if(!empty($_POST) && !empty($_GET)) {
$encoded = http_build_query($_GET) . "&" . http_build_query($_POST);
} elseif (empty($_POST) && !empty($_GET)) {
$encoded = http_build_query($_GET);
} elseif (!empty($_POST) && empty($_GET)) {
$encoded = http_build_query($_POST);
} else {
$encoded = "";
}
$encoded = http_build_query($_GET) . "&" . http_build_query($_POST);
preg_match('/!\d+!/', $encoded, $zip);
print_r($zip);
$ch = curl_init("http://lookup.cla.base8tech.com/"); // Set cURL url
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // Send request via POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded); // Set POST data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // curl_exec() returns response
curl_setopt($ch, CURLOPT_HEADER, "Content-Type: application/x-www-form-urlencoded");
$response = curl_exec($ch); // Return the request response
curl_close($ch);