Convert cUrl comnad lines to PHP lines - php

I have a curl command that I can successfully run from a Linux (ubuntu) command line.
I would need to incorporate this curl command in a PHP script.
How can I translate the below curl command so that it works in a PHP script?
curl -X POST -H "Accept: application/rdf+xml" -F recipe=http://demo/recipe_alta -F input=#prova_rdf.rdf http://site.cs.unibo.it/stanbol/refactor
I've been trying a lot of calls in PHP I've found on the internet but I always get http 409 and 415 messages.
I tried also exec and shell_exec but I get the same errors.
this is another call, just for posting the rdf file on the url, and i get the same errors.
$url = "http://bernina.cs.unibo.it/stanbol/triplestore/grafotest_cavo";
$post_data['file'] = "#prova_rdf.rdf";
$ch = curl_init();
$headers = array('Accept: application/rdf+xml');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$response = curl_exec($ch);
echo $response;

Related

Adding Product Images in Shopify using CURL In PHP - Cloudflare Errors

I'm trying to add images to my products using CURL in PHP based on this: https://shopify.dev/api/admin-rest/2022-07/resources/product-image#post-products-product-id-images
This is what it should look like using CURL CLI but I'm trying to use it through PHP:
curl -d '{"image":{"src":"http://example.com/rails_logo.gif"}}' \
-X POST "https://your-development-store.myshopify.com/admin/api/2022-07/products/632910392/images.json" \
-H "X-Shopify-Access-Token: {access_token}" \
-H "Content-Type: application/json"
I've tried a number of different format but keep getting Cloudflare errors with the below format:
$ch = curl_init("https://STORENAME.myshopify.com/admin/api/2022-07/products/632910392/images.json");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Shopify-Access-Token: SECRETTOKEN"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_URL, "http://example.com/rails_logo.gif");
curl_setopt($ch, CURLOPT_INFILE, "http://example.com/rails_logo.gif");
curl_setopt($ch, CURLOPT_POSTFIELDS,'{"image":{"position":'1',"src":"http://example.com/rails_logo.gif"}}');
$responseTmp = curl_exec($ch);
I'm getting "BAD REQUEST" and "1020" errors from Cloudflare, I assume because something in my request isn't formatted correctly.
Would appreciate any advice.
In case it helps someone else further down the track - this is how Shopify expects image uploads via php curl:
$image = json_encode(array('image'=> array('src' => $imageUrl)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $image);

How to get releases information from github using php-curl (with credentials)?

I am able fetch github release details using curl but I want to do this using php.
The command I use in curl is the following:
curl -u user.ca:password -X GET https://api.github.com/repos/xyz/abc/releases
The php equivalent of the -u option is CURLOPT_USERPWD.
curl_setopt($ch, CURLOPT_USERPWD, 'user.ca:password');
In the end you will have something like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.github.com/repos/xyz/abc/releases");
curl_setopt($ch, CURLOPT_USERPWD, 'user.ca:password');
curl_setopt($ch, CURLOPT_USERAGENT,'Awesome-Octocat-App');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$fetch = curl_exec($ch);
curl_close($ch);

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 request from shell

I have simple php script.
$url = 'http://test.com/api/images/products/33';
$image_path = '/srv/images/some.jpg';
$key = 'qwerty';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_PUT, true); // Un-commet to edit an image
curl_setopt($ch, CURLOPT_USERPWD, $key.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '#'.$image_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
The script works well. I want to convert this code to simple curl shell command.
I tried
curl -v -X POST -d image=#/srv/images/some.jpg \
http://qwerty#test.com/api/images/products/33
but error is occured. What is wrong?
you should use -F (for multipart/form-data) instead of -d (which uses application/x-www-form-urlencoded):
so this should work:
curl -v -X POST -F image=#/srv/images/some.jpg http://qwerty#test.com/api/images/products/33

Categories