convert curl line command in php code - php

can anyone help me to convert that curl comand line in a php curl code ? plz
curl -v --data "WSCommunityStringRW?2=1200ve50set&Submit=Submit" http://xxxxx/123 -u "admin:a1s2d3" --anyauth
Yes.I did something but ... not work :(
$api_url = 'xxx/123';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "admin:a1s2d3");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'WSCommunityStringRW?2' => '1200ve50set',
'Submit' => 'Submit'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_close($ch);

Have you taken a look at the cURL pages in the PHP manual? It should contain everything you need for this.
Edit
In the code you've provided, curl_exec() is missing. If it doesn't work after calling curl_exec(), try looking at the output of curl_error().

Related

Converting Linux Curl Command to PHP

I want to know, how to convert linux curl command to PHP. This is my linux curl command. Thanks in advance.
curl -i -F api_password=<YOUR_API_PASSWORD> -F file=#<LOCAL_FILE_PATH> https://upload.wistia.com/
Here is the starting point that you can use...
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $THE_REMOTE_URL_YOU_ARE_POSTING_TO);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"file" => "#c:\\file_location.txt", // note the double \\ when used within double quotes
'api_password' => 12345
));
$response = curl_exec($ch);
?>

Converting a Curl command into PHP Curl code (file transfer & custom command)

I am trying to convert the following Curl command:
curl --digest --user "username:password" --verbose --url "http://127.0.0.1/ws?graph-uri=http://localhost/dataset/import/" -X POST -T /data/datasets/foo.n3
Here is the code that appears to be ok, but that doesn't work:
$args = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1/ws?graph-uri=http://localhost/dataset/import/');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$args['graph-uri'] = curl_file_create('/data/datasets/foo.n3');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$result = curl_exec($ch);
It seems that the file is actually not uploaded with the PHP code, but everywhere I look the usage of curl_file_create() seems good.
Try this previous answer, substituting graph-uri for file_contents. Also, you are setting "graph-uri" as both GET and POST params, which could collide, and the graph-uri GET param is not URL encoded, which could behave differently in the shell vs. PHP. So you might try the following:
http://127.0.0.1/ws?graph-uri=http%3A%2F%2Flocalhost%2Fdataset%2Fimport%2F

How do I extract data/results from a cURL command to display the output with PHP?

First off, iam quite new with both PHP and cURL
From the command:
curl -i 'http://xxx:xxx/v2.0/tokens' -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d '{"auth": {"tenantName": "xxx", "passwordCredentials": {"username": "admin", "password": "xxx"}}}'
I want to produce the results of this command using POST/GET and PHP. I have an apache server and working url.
I just dont know how to make a working code that takes the code and produce the output to an empty page using PHP.
I have been searching examples and I know you got to make use of some of the following:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
?>
I just dont know how to attach the proper flags from the cURL command to the PHP code, probably missing a few vital things aswell to wrap the code up.
Thanks in advance and sorry if I could not explain the my problem good enough.
** ***EDIT:* ****
So I made a code, that works for my needs, it produces a raw result from the cURL command on a PHP page.
Here is the code:
<?php
$request_body = '{"auth": {"tenantName": "xxx", "passwordCredentials": {"username": "admin", "password": "xxx"}}}';
$ch = curl_init('http://xxx:xxx/v2.0/tokens');
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$response = curl_exec($ch);
var_dump($response);
?>
Question now if I can manipulate the output to display in a better fashion, right now its just a big string, some form of JSON pretty print would be amazing.
Read more about curl exec (http://www.php.net/manual/en/function.curl-exec.php)
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, '');
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
print $data;
?>
The data is returned from the curl_exec command.
Whilst on a different subject, perhaps Uploading files to Zoho API with PHP Curl, a question I posted here recently which contains a Curl class example will help you.
In addition, I suggest you read the manual pages for the PHP curl methods e.g. the curl_exec page - the comments sections often include implementation suggestions.

Convert curl command to PHP code

I need to delete opencast matterhorn recording via REST api which they already provided. (just for the information, no need to worry about matterhorn)
I need to develop simple PHP code to DELETE some entries via given REST API. I have tested with curl command line it is working fine, but I can't convert that into working PHP code.
working curl command :
curl --digest -X "DELETE" -u matterhorn_system_account:CHANGE_ME -H "X-Requested-Auth: Digest" -H "X-Opencast-Matterhorn-Authorization: true" url/search/xxxx
not working PHP command :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'url/search/xxxx');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, 'matterhorn_system_account:CHANGE_ME');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-Auth: Digest"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Opencast-Matterhorn-Authorization: true"));
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
result $httpCode is 302, means it's not working.
Any idea where I went wrong.
Thanks in advance
You need to combine these two so you aren't overwriting yourself:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-Auth: Digest"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Opencast-Matterhorn-Authorization: true"));
So that should be
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-Auth: Digest",
"X-Opencast-Matterhorn-Authorization: true"));
And it probably would also help to follow redirects with
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl api isnt responding anything

I am trying to write PHP code for following API:
curl -u ********* -d email=tester#gmail.com \
--data-urlencode msg="First message from domain API" \
--data-urlencode url="http://www.domain.com/welcome" \
https://api.domain.com/1/send
PHP Code:
<?php
$url = "https://api.domain.com/1/send ";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_USERPWD, "*********");
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, "email=test#outlook.com&msg=hi%url=http://yahoo.com/"); // add POST fields
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
What am I doing wrong to process the above API?
Try this:
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode("email=test#outlook.com&msg=hi&url=http://yahoo.com/"));
There may be other errors (I can't easily test your code) but that should help. Notice that, in addition to adding the urlencode function, I also changed a % to a &
This question will probably help you, too.

Categories