简单的卷曲GET
要求:(还增加了JSON /头如果需要的话,让您的生活需要更容易)
<?php
$chTest = curl_init();
curl_setopt($chTest, CURLOPT_URL, "http://example.com:8080/?query=Hello+World");
curl_setopt($chTest, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Accept: application/json'));
curl_setopt($chTest, CURLOPT_HEADER, true);
curl_setopt($chTest, CURLOPT_RETURNTRANSFER, true);
$curl_res_test = curl_exec($chTest);
$json_res_test = explode("\r\n", $curl_res_test);
$codeTest = curl_getinfo($chTest, CURLINFO_HTTP_CODE);
curl_close($chTest);
?>
例POST
请求:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com:8080');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Accept: application/json'));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"Hello" : "World", "Foo": "World"}');
// Set timeout to close connection. Just for not waiting long.
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$curl_res = curl_exec($ch);
?>
字符集不是必需的HTTP标头其他人也是如此,重要的是Content-Type。
有关的例子,我用JSON的MIME类型,你可以使用任何你想要的,看看链接: http://en.wikipedia.org/wiki/Internet_media_type
确保php_curl.dll
扩展你的PHP启用,也是港口在目标服务器上打开。
希望这会有所帮助。
您可以尝试http://somesite.tld:8080作为url。 – TJHeuvel 2011-06-09 08:12:28
你正在使用哪个托管? – Kamil 2011-06-09 08:13:16
orbit.theplanet.com – VeeBee 2011-06-09 08:14:39