سؤال

I am new to web programming and am wondering how to go about solving this small problem.

http://wokinfo.com/cgi-bin/dci/search.cgi

on the following webpage there is a "post" form available for searching. I can see that following name=value pair exists "search=university" ( just for example ) but when i try to do this using phpcurl it does'nt seem to work. Here is my simple code.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://wokinfo.com/cgi-bin/dci/search.cgi");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
        "search=university");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
print curl_getinfo($ch, CURLINFO_HTTP_CODE) . "<br>";
print $output . "\n";
curl_close($ch);

What am i doing wrong?

هل كانت مفيدة؟

المحلول 2

You need to set the values of CURLOPT_POSTFIELDS as an array like this:

curl_setopt($ch, CURLOPT_POSTFIELDS, array("search" => "university"));

While the documentation states that you can add params as a string, that always seems messy & error prone. Passing an array is cleaner. Here it is in your code:

// Set the `post` fields.
$post_fields = array();
$post_fields['search'] = 'university';
$post_fields['searchtype'] = 'and';
$post_fields['client'] = 'default';
$post_fields['output'] = 'xml_no_dtd';
$post_fields['proxystylesheet'] = 'default';

// Core curl logic.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://wokinfo.com/cgi-bin/dci/search.cgi");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
print curl_getinfo($ch, CURLINFO_HTTP_CODE) . "<br>";
print $output . "\n";
curl_close($ch);

نصائح أخرى

You forgot to send the searchtype parameter:

curl_setopt($ch, CURLOPT_POSTFIELDS, "search=university&searchtype=and");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top