Nasıl PHP isteği ve yargıç istek türünü SİL / GET / POST / PUT başlatmak için?

2 Cevap php

Ben, PUT/DELETE isteği gönderilir nasıl görmek asla

PHP bunu nasıl?

Ben curl ile bir GET / POST isteği göndermek için biliyorum:

$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookieFile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,   CURLOPT_SSL_VERIFYPEER,   FALSE);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);

Ama nasıl isteği SİL / PUT yaparsınız?

2 Cevap

For DELETE use curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
For PUT use curl_setopt($ch, CURLOPT_PUT, true);

CURL yüklü dayanmaz alternatif bir custom HTTP stream context ile file_get_contents kullanmak olacaktır.

$result = file_get_contents(
    'http://example.com/submit.php', 
    false, 
    stream_context_create(array(
        'http' => array(
            'method' => 'DELETE' 
        )
    ))
);

PHP ile dinlenme yapıyor bu iki makalelere göz atın

Eğer bazı "non-GET" isteği göndermek istiyorsanız Genellikle konuşma, sık sık curl ile çalışacağız.


And you'll use the curl_setopt function to configure the request you're sending ; amongst the large amount of possible options, to change the request method, you'll be interested by at least those options (quoting) :

  • CURLOPT_CUSTOMREQUEST: yerine "GET" kullanımı bir özel istek yöntemi veya "HEAD" Bir HTTP isteği yaparken. Bunu yapmak için yararlıdır "DELETE" ya da diğer, daha karanlık HTTP istekleri.
  • CURLOPT_HTTPGET: TRUE GET için HTTP isteği yöntemi sıfırlamak için.
  • CURLOPT_POST: TRUE düzenli bir HTTP yapmak için POST.
  • CURLOPT_PUT: TRUE HTTP PUT bir dosya. Için dosya PUT CURLOPT_INFILE ve CURLOPT_INFILESIZE ile ayarlanmalıdır.


Of course, curl_setopt is not the only function you'll use ; see the documentation page of curl_exec for an example of how to send a request with curl.

(Yes, that example is pretty simple, and sends a GET isteği - ama) oradan ;-) inşa etmek gerekir