I use Stanford NLP local server by a curl call of
curl --data "this is a test" http://localhost:9001/?properties={%22prettyPrint%22%3A%22true%22}
and everything works perfectly. When I try to use the equivalent curl command in PHP as
$ch = curl_init('http://localhost:9001/?properties={%22prettyPrint%22%3A%22true%22}');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'this is a test');
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
I get an error of
string(49) "<h1>400 Bad Request</h1>URISyntaxException thrown"
I reckon the problem is the format of POSTFIELDS, but still no luck after trying different variations of an array.
You need to tell curl to do a regular post. Just add this line before CURLOPT_POSTFIELDS assigment:
curl_setopt($ch, CURLOPT_POST, true);
Related
I'm trying to implement the AAA Cooper Freight's SOAP API via PHP.
When I send XML request to http://wsportal.aaacooper.com:8188/wsportal20/wsGenEst, it via Postman, works fine, but when using CURL, it doesn't return anything
I use the direct url(from the wsdl file), because their WSDL file seems to be broken and its not working with it:
http://wsportal.aaacooper.com:8188/wsGenRateEstimate.wsdl
Here is screenshot from Postman(Working!)
and my php code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://wsportal.aaacooper.com:8188/wsportal20/wsGenEst" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $input_xml );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=ISO-8859-1'));
$data = curl_exec($ch);
print_r($data);
curl_close($ch);
I'm using same XML string on both places.
Postman can generate the PHP code for any given request using the Code button on the top right corner of the screen. A demonstration of this feature can be seen in the image below.
Triggering Jenkins job via following PHP script:
<?php
$testrun_id = "1744";
$cmd = "curl -X POST http://build:f9280f75396f83a0#mobile-jenkins.domain.com:8080/job/android-test/build --data-urlencode json='{\"parameter\": [{\"name\":\"POST_RESULTS\", \"value\":\"true\"}, {\"name\":\"RUN_ID\", \"value\":\"{$testrun_id}\"}, {\"name\":\"CHECK_NAME\", \"value\":\"SampleAutomatedPlan\"}]}'";
exec($cmd,$result);
?>
This script runs successfully on Mac and the jenkins job does get triggered. How do I make this script to work on Windows? I am getting following error when I run above PHP script on Windows?
curl is already installed on windows machine. Also, is there a better way to do cURL in PHP? Looking at this: http://php.net/manual/en/book.curl.php, can someone point me towards an example based on my curl command in the above PHP script(for Windows)? An example based on the curl command in my script would be ideal.
you should check examples from here http://php.net/manual/en/curl.examples.php
Bellow is the code for you case,
$url = "http://build:f9280f75396f83a0#mobile-jenkins.domain.com:8080/job/android-test/buildWithParameters";
$data = "POST_RESULTS=true&RUN_ID=".$testrun_id."&CHECK_NAME=SampleAutomatedPlan";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
You need to set the content type for JSON
curl -H "Content-Type: application/json" -X POST http://build:f9280f75396f83a0#mobile-jenkins.domain.com:8080/job/android-test/build --data-urlencode json='{\"parameter\": [{\"name\":\"POST_RESULTS\", \"value\":\"true\"}, {\"name\":\"RUN_ID\", \"value\":\"{$testrun_id}\"}, {\"name\":\"CHECK_NAME\", \"value\":\"SampleAutomatedPlan\"}]}'";
Just make sure you don't have any mix matched values.
I'm integrating with a 3rd party's API, I have to POST some XML and I get some XML back.
On the CLI this works, I get a positive response.
curl -X POST -d #/tmp/file http://url/to/endpoint --header "Content-Type:application/x-www-form-urlencoded"
This, however, does not work, the response contains an error telling me my that my request XML is invalid.
$ch = curl_init();
$post = array(
'file' => '#/tmp/file'
);
curl_setopt($ch, CURLOPT_URL, 'http://url/to/endpoint');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$this->responseBody = curl_exec($ch);
curl_close($ch);
It's the same file in both cases and it's on the same server. the file is just plain text XML. The only difference that I can see is that I'm specifying a fieldname in my HTTP headers on the PHP version.
How do I send that file over using PHP to exactly replicate the CLI version, e.g. without the formdata/fieldname bit?
FWIW I can't go back to the developer of the API for a few days to ask what he's defining as 'bad XML'
Try passing the file as raw data, not in an array, by for example using file_get_contents().
So instead of:
$post = array('file' => '#/tmp/file');
Like this:
$post = file_get_contents('#/tmp/file');
I'm trying to fetch this url via a script: http://api.alarabiya.net/sections/2/
But JSON response received is much smaller than when I open it directly in a browser,
please notice that I tried this url through CURL and set the same USER-AGENT of the browser and all request header used in the browser and I still get a smaller response.
Here's an exmaple using just file_get_contents
<?php
echo file_get_contents("http://api.alarabiya.net/sections/2/");
?>
My question is if there's a request size limit when using file_get_contents or if the PHP's memory can't handle it or what's the problem exactly?
When I CURLed this in shell it gave me the same o/p as in php (the trimmed output).
I finally found a solution for this:
$url = "http://api.alarabiya.net/sections/2/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
// This is what solved the issue (Accepting gzip encoding)
curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
$response = curl_exec($ch);
curl_close($ch);
echo $response;
I use the following command in some old scripts:
curl -Lk "https:www.example.com/stuff/api.php?"
I then record the header into a variable and make comparisons and so forth. What I would really like to do is convert the process to PHP. I have enabled curl, openssl, and believe I have everything ready.
What I cannot seem to find is a handy translation to convert that command line syntax to the equivalent commands in PHP.
I suspect something in the order of :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// What goes here so that I just get the Location and nothing else?
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
The goal being $response = the data from the api “OK=1&ect”
Thank you
I'm a little confused by your comment:
// What goes here so that I just get the Location and nothing else?
Anyway, if you want to obtain the response body from the remote server, use:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
If you want to get the headers in the response (i.e.: what your comment might be referring to):
curl_setopt($ch, CURLOPT_HEADER, 1);
If your problem is that there is a redirection between the initial call and the response, use:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);