为什么卷曲在php中变成函数

问题描述 投票:0回答:1

我正在使代码运行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"},但是该函数不发出任何东西。

php function curl instagram-api
1个回答
0
投票

您忘了在网址末尾添加“ /”

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'));
© www.soinside.com 2019 - 2024. All rights reserved.