【发布时间】:2019-09-30 09:10:58
【问题描述】:
我正在编写代码来运行 curl,但是当我尝试将代码转换为 php 上的函数时,代码不起作用。
我在php xampp / cmd shell中运行代码,PHP版本7,我也尝试过托管,但结果是一样的。我尝试添加类似https://www.w3schools.com/php/php_functions.asp 的数据类型,但没有效果。
这是未包含在函数中的代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://i.instagram.com/api/v1/friendships/create/2878405206/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "signed_body=cdb3985eb8de0d7ab8bf9a2282615e27452b3f71c30cc95d1b1a325d9f2e36d2.%7B%22_csrftoken%22%3A%221W9jdoDp4GkJRNvWwnZ9Mm1G5Tfh2ub1%22%2C%22user_id%22%3A%222878405206%22%2C%22radio_type%22%3A%22wifi-none%22%2C%22_uid%22%3A%2221423938509%22%2C%22device_id%22%3A%22android-0aaa681cf6d47fdb%22%2C%22_uuid%22%3A%22825ebac9-3235-4cad-b187-7ed685a06b37%22%7D&ig_sig_key_version=4");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'X-Ig-Connection-Type: WIFI';
$headers[] = 'User-Agent: Instagram 24.0.0.12.201 Android (4.4.4/19; 240dpi; 800x480; GT-I9060I; samsung; samsung; grandneove3g; in_ID)';
$headers[] = 'Accept-Language: id-ID, en-US';
$headers[] = 'Host: i.instagram.com';
$headers[] = 'X-Fb-Http-Engine: Liger';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$resultLast = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print_r($resultLast);
这是作为函数使用的代码:
function sendData($path,$dataHook){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataHook);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'X-Ig-Connection-Type: WIFI';
$headers[] = 'User-Agent: Instagram 24.0.0.12.201 Android (4.4.4/19; 240dpi; 800x480; GT-I9060I; samsung; samsung; grandneove3g; in_ID)';
$headers[] = 'Accept-Language: id-ID, en-US';
$headers[] = 'Host: i.instagram.com';
$headers[] = 'X-Fb-Http-Engine: Liger';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$resultLast = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $resultLast;
}
print_r(sendData('https://i.instagram.com/api/v1/friendships/create/2878405206','signed_body=cdb3985eb8de0d7ab8bf9a2282615e27452b3f71c30cc95d1b1a325d9f2e36d2.%7B%22_csrftoken%22%3A%221W9jdoDp4GkJRNvWwnZ9Mm1G5Tfh2ub1%22%2C%22user_id%22%3A%222878405206%22%2C%22radio_type%22%3A%22wifi-none%22%2C%22_uid%22%3A%2221423938509%22%2C%22device_id%22%3A%22android-0aaa681cf6d47fdb%22%2C%22_uuid%22%3A%22825ebac9-3235-4cad-b187-7ed685a06b37%22%7D&ig_sig_key_version=4'));
它应该产生{"message": "login_required", "error_title": "You have logged out", "error_body": "Please log in again.", "logout_reason": 2, "status": "fail"},但该函数没有发出任何东西。
【问题讨论】:
-
but the function does not issue anything. -
我认为将代码转化为函数根本不是问题。您的签名正文中有一个
_csrftoken。令牌是否可能已过期? -
@KoalaYeung _csrftoken 可以过期,但是如果你使用它,它仍然会返回结果
-
@treyBake 你是什么意思?
-
@Kurcaci213 如果您启用了错误,它可能会告诉您代码本身是否有问题。我跳到这里是因为如果它没有发出任何东西,那么它可能是 PHP 方面的东西,而不是“因为它在一个函数中”
标签: php function curl instagram-api