需要一些关于 php curl 的信息
need some information about php curl
请在我提问之前先看看那些数据。
API URL: https://api.awebsite.com/api/redeem
数据发送方式:POST
请求Headers:
Host: api.awebsite.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0
Accept: application/json
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://m.awebsite.com/en/exchange
Content-Type: application/json
Content-Length: 87
Origin: https://m.awebsite.com
Connection: keep-alive
Cookie: PHPSESSID=e0c4f6ec8a13e963bf6b11ebc33a96d2
TE: Trailers
Pragma: no-cache
Cache-Control: no-cache
发布数据
{"redeemcode":"f564hfkj4shfee25","gameid":"123456","vcode":"7895","language":"en"}
我从浏览器 > 检查 > 网络区域收集了所有这些。
我的问题是,我可以使用 php curl 到 post 数据到 api url 从我的本地主机或我的服务器吗?我写了自己的代码,但它不起作用。这是我的代码。
//API Url
$url = 'https://api.awebsite.com/api/redeem';
$code = 'f564hfkj4shfee25';
$user = '123456';
$vcode = '7895';
//Initiate cURL.
//$ch = curl_init();
//The JSON data.
$jsonData = array(
'redeemcode' => $code,
'gameid' => $user,
'vcode' => $vcode,
'language' => 'en'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'https://m.awebsite.com/en/exchange');
//Execute the request
$result = curl_exec($ch);
您认为可以使用 php 来 post 数据吗?
是的,您可以使用 cURL 将 post 发送到其他域,但是...其他域 (https://api.awebsite.com/api/redeem) 需要使用跨域策略允许访问
<?PHP
//API Url
$url = 'https://api.awebsite.com/api/redeem';
$code = 'f564hfkj4shfee25';
$user = '123456';
$vcode = '7895';
//Initiate cURL.
//$ch = curl_init();
//The JSON data.
$jsonData = array(
'redeemcode' => $code,
'gameid' => $user,
'vcode' => $vcode,
'language' => 'en'
);
$ch = curl_init();
//curl_setopt($ch, CURLOPT_URL, $url);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
$defaults = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonDataEncoded,
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true,
CURLOPT_SSL_VERIFYPEER => false // <= Skip the secure validation
);
curl_setopt_array($ch, ($defaults));
//Execute the request
echo $result = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($info);
curl_close($ch);
请在我提问之前先看看那些数据。
API URL: https://api.awebsite.com/api/redeem
数据发送方式:POST
请求Headers:
Host: api.awebsite.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0
Accept: application/json
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://m.awebsite.com/en/exchange
Content-Type: application/json
Content-Length: 87
Origin: https://m.awebsite.com
Connection: keep-alive
Cookie: PHPSESSID=e0c4f6ec8a13e963bf6b11ebc33a96d2
TE: Trailers
Pragma: no-cache
Cache-Control: no-cache
发布数据
{"redeemcode":"f564hfkj4shfee25","gameid":"123456","vcode":"7895","language":"en"}
我从浏览器 > 检查 > 网络区域收集了所有这些。
我的问题是,我可以使用 php curl 到 post 数据到 api url 从我的本地主机或我的服务器吗?我写了自己的代码,但它不起作用。这是我的代码。
//API Url
$url = 'https://api.awebsite.com/api/redeem';
$code = 'f564hfkj4shfee25';
$user = '123456';
$vcode = '7895';
//Initiate cURL.
//$ch = curl_init();
//The JSON data.
$jsonData = array(
'redeemcode' => $code,
'gameid' => $user,
'vcode' => $vcode,
'language' => 'en'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'https://m.awebsite.com/en/exchange');
//Execute the request
$result = curl_exec($ch);
您认为可以使用 php 来 post 数据吗?
是的,您可以使用 cURL 将 post 发送到其他域,但是...其他域 (https://api.awebsite.com/api/redeem) 需要使用跨域策略允许访问
<?PHP
//API Url
$url = 'https://api.awebsite.com/api/redeem';
$code = 'f564hfkj4shfee25';
$user = '123456';
$vcode = '7895';
//Initiate cURL.
//$ch = curl_init();
//The JSON data.
$jsonData = array(
'redeemcode' => $code,
'gameid' => $user,
'vcode' => $vcode,
'language' => 'en'
);
$ch = curl_init();
//curl_setopt($ch, CURLOPT_URL, $url);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);
$defaults = array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonDataEncoded,
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true,
CURLOPT_SSL_VERIFYPEER => false // <= Skip the secure validation
);
curl_setopt_array($ch, ($defaults));
//Execute the request
echo $result = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($info);
curl_close($ch);