あなたのコードにいくつかのエラーがあります。まず、あなたの主な質問に答えるために、あなたは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);
を使用します。cURL
を使用してPOST
リクエストを送信し、応答を取得するには、次のコードを使用します。これは必須ではなく、代わりに$encoded = substr($encoded, 0, -1);
としてください。
第3に、正規表現は現在無効です。あなたは、文字列の最初と最後に/
を追加する必要があります: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);
を