我正在使用PHP的基本REST应用程序,我有以下代码。 客户端代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch , CURLOPT_HEADER,false);
curl_setopt($ch , CURLOPT_FOLLOWLOCATION , true);
curl_setopt($ch , CURLOPT_URL , "http://localhost/Hello/Rest");
curl_setopt($ch , CURLOPT_POSTFIELDS,
http_build_query(array("username" => "test")));
$output = curl_exec($ch);
print_r($output);
curl_close($ch);
服务器端代码:
$username = $_REQUEST['username'];
echo "This is a PUT request";
echo "<br>";
echo $username;
echo "<br>";
由于某种原因,无法识别$ _REQUEST ['username']。生成未定义的索引。
不确定导致此错误的遗漏内容。
答案 0 :(得分:4)
正如@NaijaProgrammer指出的那样,$ _REQUEST不包含PUT值。如果您想坚持使用PUT,则需要修改服务器代码。请参阅此链接for more information.
import os,sys,bs4,random,codecs,requests
import unicodecsv as csv
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from contextlib import contextmanager
from selenium.webdriver.support.expected_conditions import staleness_of
from selenium.webdriver.support import expected_conditions as EC
current_file = sys.argv[0]
link_dir = os.path.dirname(current_file)
link_path = os.path.join(link_dir,'lnks.txt')
Image_folder = os.path.join(link_dir,"images")+"\\"
urls = [line.strip() for line in open(link_path, 'r')]
urls = list(set(urls))
url = urls[0]
driver = webdriver.Firefox()#Chrome()##chromedriver)##
base_url = 'http://www.hotleathers.com'
Header = [u'Url',u'Name',u'Sku',u'Price',u'Color',u'Size']
#def get_data(url):
#try:
print "Scraping : %s"%url
driver.get(url)
driver.implicitly_wait(3)
detpage_lnks = driver.find_elements_by_xpath("//div[@style='margin-top:0px;margin-bottom:5px']/a")
detpage_lnks = map(lambda x: x.get_attribute('href'),detpage_lnks)
for i in detpage_lnks:
Data = []
#try:
driver.get(i)
driver.implicitly_wait(3)
Name_v=driver.find_element_by_xpath("//table [@class='showproductpage']/tbody/tr/td/h1").text
Sku_v=driver.find_element_by_xpath("(//table[@cellspacing = '0'])[3]//td[@style='padding-left:5px; font-size:16px; font-weight:bold;']").text
image_name = Sku_v+".jpg"
image_url = "http://www.hotleathers.com/Assets/ProductImages/large/"+image_name
res = requests.get(image_url)
if res.status_code == requests.codes.ok:
out = open(Image_folder+image_name,'wb')
out.write(res.content)
Price_v=driver.find_element_by_xpath("((//table[@cellspacing = '0'])[3]//tr)[2]//span").text
Color=driver.find_elements_by_xpath("(//table[@class='buyProductForm'])//tr[2]/td/select/option")
Color_v = '"'+':'.join([i.text for i in Color[1:]])+'"'
Size=driver.find_elements_by_xpath("(//table[@class='buyProductForm'])//tr[3]/td/select/option")
Size_v = '"'+':'.join([i.text for i in Size[1:]])+'"'
temp = [driver.current_url,Name_v,Sku_v,Price_v,Color_v,Size_v]
Data.append(zip(Header,temp))
Data = [item for sublst in Data for item in sublst]
my_dict = dict(Data)
with codecs.open(os.path.join(link_dir,"Image_info.csv"),'ab',encoding="utf-8") as f:
# Using dictionary keys as fieldnames for the CSV file header
writer = csv.DictWriter(f,fieldnames=my_dict.keys())
writer.writerow(my_dict)
driver.close()
答案 1 :(得分:-4)
你必须退回你的$ output
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch , CURLOPT_HEADER,false);
curl_setopt($ch , CURLOPT_FOLLOWLOCATION , true);
curl_setopt($ch , CURLOPT_URL , "http://localhost/Hello/Rest");
curl_setopt($ch , CURLOPT_POSTFIELDS,
http_build_query(array("username" => "test")));
$output = curl_exec($ch);
curl_close ($ch);
return $output;