簡體   English   中英

嘗試使用php和curl復制Web請求(圖像上傳)

[英]Trying to replicate web request (image upload) using php and curl

我正在嘗試將圖片上傳到網站,但該網站沒有為此提供api函數。 我設法使用Charles Proxy獲取請求信息:

Charles Proxy屏幕截圖

這是我的PHP代碼:

$post_data = array(
    'photo' => '@'.$filename,
    '_csrftoken' => '5ebcec201972ab6304a33d418129cd13',
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/api/v1/upload/photo/');
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Host: example.com'
));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'C:/xampp/htdocs/example/cookies.txt');            


$response = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

print_r($response);

echo $http;

這將返回http代碼為500的響應。

您沒有正確發布。

您不需要Charles Proxy

在進行上傳(chrome,firefox)之前,

  • 右鍵單擊選擇檢查元素
  • 選擇網絡標簽
  • 刷新頁面
  • 選擇文檔(chrome)或HTML(firefox)
  • 清除清單
  • 發布您的上傳
  • 在請求列表中選擇上傳請求
  • 在fireFox中,選擇“編輯並重新發送”,在Chrome中,選擇“查看源代碼”

在右側,它將顯示請求和響應頭

您需要使您的請求看起來完全像那個請求標題

您必須注意重定向(例如302)和在重定向過程中添加的Cookie。

您將要查看“請求和響應頭”,以防無法解決問題。

curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_FAILONERROR,true);

您可能想要獲取Cookie。 創建另一個curl請求以獲取上載頁面。

捕獲cookie:在curl請求上傳頁面時獲得Response標頭($ head)

$data = curl_exec($ch);

if (curl_errno($ch)){
    $data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
  $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
  $head = substr($data,0,$skip);
  $e = 0;
  while(true){
    $s = strpos($head,'Set-Cookie: ',$e);
    if (!$s){break;}
    $s += 12;
    $e = strpos($head,';',$s);
    $cookie = substr($head,$s,$e-$s) ;
    $s = strpos($cookie,'=');
    $key = substr($cookie,0,$s);
    $value = substr($cookie,$s);
    $cookies[$key] = $value;

  }

格式化捕獲的上傳請求:

 $cookie = '';
 $show = '';
 $head = '';
 $delim = '';
 foreach ($cookies as $k => $v){
   $cookie .= "$delim$k$v";
   $delim = '; ';
 }

您需要為卷曲添加一些選項

創建POST數據字符串

$post = 'key1=value1&key2=value2&key3=value3';

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

創建一個數組以放置請求標頭鍵值
使用您上載的Request標頭中的內容完全填寫Request數組。
例:

$request = array();
$request[] = "Host: www.example.com";
$request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";

添加到卷曲:

curl_setopt($ch, CURLOPT_HTTPHEADER, $request);

將follow設置為false。 如果有重定向,您可以查看正在發生的情況。 然后向重定向位置創建另一個curl請求。

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

在上傳curl請求Request之后,獲取Header:

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FILETIME, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
$data = curl_exec($ch);
if (curl_errno($ch)){
    $data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
  $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
  $head = substr($data,0,$skip);
  $data = substr($data,$skip);
  $info = curl_getinfo($ch);
  $info = var_export($info,true);
}
echo $head;
echo $info;

如果無法正常工作,請檢查$ info中“請求標頭”中的差異。

暫無
暫無

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

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