簡體   English   中英

使用curl不帶命令行,但使用php作為post方法

[英]using curl without command line, but with php for a post method

我需要從php執行http post / curl查詢,並將結果存儲在jsor變量中。

在我的文檔中,它說有兩種方法:

  • HTTP POST請求,其內容類型為“ multipart / form-data”,其中所有參數都在帖子主體中,並且軌道在帖子“文件”的“軌道”部分中
  • 內容類型為“ application / octet-stream”的HTTP POST請求,其中本地文件為請求的主體,URL中的參數

示例POST:

curl -X POST“ http://developer.echonest.com/api/v4/track/upload ” -d“ api_key = xxxxxx&url = http://example.com/audio.mp3

但是如何在php中實現呢? 我沒有任何線索。 從我的閱讀中,我不認為curl_init方法會像在php網站上描述的那樣起作用,因為它不是post方法:

$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);

我歡迎任何指示如何進行。 謝謝。

這樣呢

$post = array(
     "url"=>"path/to/file/example_homepage.txt",
     "api_key"=>"xxxxxx"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "http://developer.echonest.com/api/v4/track/upload");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
$response = curl_exec($ch);

還有其他選項,具體取決於您可能需要的情況,SSL,Cookie,用戶代理等。 這是php參考的鏈接!

暫無
暫無

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

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