簡體   English   中英

使用Curl PHP的POST URL

[英]POST url with Curl PHP

我有一個感謝頁面,其中的URL包含變量:

http://vieillemethodecorpsneuf.com/confirmation-achat-1a/?item=1&cbreceipt=VM6JQ6VE&time=1429212702&cbpop=C123FA24&cbaffi=twitpalace&cname=Roberto+Laplante&cemail=roberto%40gmail.com&ccountry=FR&czip=000

我有這個GET函數來捕獲變量:

<?php
$clickbank_name = (isset($_GET['cname'])) ? $_GET['cname'] : ''; 
$clickbank_email = (isset($_GET['cemail'])) ? $_GET['cemail'] : '';
$clickbank_country = (isset($_GET['ccountry'])) ? $_GET['ccountry'] : '';
$clickbank_zip = (isset($_GET['czip'])) ? $_GET['czip'] : '';
$clickbank_aff = (isset($_GET['cbaffi'])) ? $_GET['cbaffi'] : '';
?>

現在,我需要使用curl PHP將數據發送到該Zapier URL(但附加了變量,這樣它將給我):

https://zapier.com/hooks/catch/bheq6y/?tag=client&cbaffi=twitpalace&cname=Roberto+Laplante&cemail=roberto%40gmail.com&ccountry=FR&czip=000

PS。 我在網址中添加了一個手動標記

使該工作正常的PHP Curl代碼是什么? 需要在后台進行操作。

我會試一試。

$get_fields = ['tag' => 'client'];
if (isset($_GET['cname'])) $get_fields['cname'] = $_GET['cname'];
if (isset($_GET['cemail'])) $get_fields['cemail'] = $_GET['cemail'];
if (isset($_GET['ccountry'])) $get_fields['ccountry'] = $_GET['ccountry'];
if (isset($_GET['czip'])) $get_fields['czip'] = $_GET['czip'];
if (isset($_GET['cbaffi'])) $get_fields['cbaffi'] = $_GET['cbaffi'];

$encoded = '';
foreach($get_fields as $name => $value){
    $encoded .= urlencode($name).'='.urlencode($value).'&';
}

$url = 'https://zapier.com/hooks/catch/bheq6y/?'.rtrim($encoded,'&');

// simple get curl
$output = file_get_contents($url);

// or if you want more control over the request
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $url,
));
$output = curl_exec($curl);
curl_close($curl);

在此示例中,我們將數據以參數的形式發布到另一個curl.php頁面和curl.php中,每當curl.php被調用時,我都會編寫一些代碼來執行,並將結果從curl.php得到的地方發送回頁面一個要求。 機制:此示例是使用Curl在PHP中制作的。

第一步:創建curl.php文件。 通過使用curl機制,該文件將被另一個php文件調用。 因此,在curl.php中,我們將獲得一些參數。 我們獲取參數並執行一些功能。 之后,當我們需要輸出時,我們僅使用json_encode()和簡單的echo來對json進行編碼。

$post = $_POST;
echo json_encode($post);

注意:index.php將以json格式獲取數據,因為我們會將json格式的數據發送回index.php

第二步:我們必須創建一個index.php文件。 在這里,我們編寫了一個用於執行帶有某些參數的curl的邏輯,然后調用curl.php並從curl.php獲取結果。$ url =' http://localhost/curl_demo/curl.php '; //這是我要在curl執行時執行的目標文件(curl.php)$ ch = curl_init($ url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'id=1&name=sanjay'); // pass parameters to curl.php
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
$resArr = json_decode($response, true);
curl_close($ch);
print_r($resArr);

注意:curl成功執行后獲得的數據將以json格式獲得。 這意味着我們必須使用php中的json_decode()對其進行解碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM