Debugging curl requests php

PHP — Debugging Curl

When CURLOPT_VERBOSE is set, output is written to STDERR or the file specified using CURLOPT_STDERR . The output is very informative.

You can also use tcpdump or wireshark to watch the network traffic.

How to debug curl in PHP?

Add the following code in the start and try again.

ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(E_ALL);

And if there is any parsing issue, then you will have to edit php.ini file and set this param

How to debug a get request in php using curl

Add a few more option for troubleshooting purposes.

Check for an error response.

If no error, get the details:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);

$data = curl_exec($ch);
if (curl_errno($ch)) $data .= 'Retreive Base Page Error: ' . curl_error($ch);
>
else $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$head = substr($data,0,$skip);
$data = substr($data,$skip);
$info = curl_getinfo($ch);
$info = var_export($info,true);
>
echo $head;
echo $info;

How can I debug PHP cURL not triggering a request?

SELinux is preventing the httpd process from making a network connection.

setsebool -P httpd_can_network_connect 1

See the httpd_selinux(8) man page for details.

Steps for debugging php Curl on windows

Here is a list of steps for debugging Curl:

Check in phpinfo module that Curl IS enabled.

Verify extensions dir path is propperly set in php.ini and that extension=php_curl.dll is uncommented.

Check that Environment Variables are propperly set as per: http://php.net/manual/en/faq.installation.php#faq.installation.addtopath

Run deplister.exe ext\php_curl.dll to verify all dependencies are correctly satisfied.

If all of the above is working then check the output of CURLOPT_VERBOSE. As per @hp95 suggestion in the following thread: No Response getting from Curl Request to https server

If you are reaching a site that uses SSL check the following post: HTTPS and SSL3_GET_SERVER_CERTIFICATE:certificate verify failed, CA is OK

CURLOPT_VERBOSE not working

CURLOPT_STDERR must be set to something specific.

Setting curl_setopt($c, CURLOPT_STDERR, fopen(‘/curl.txt’, ‘w+’)); fixed my issue.

As it turns out curl_setopt($c, CURLOPT_VERBOSE, 1); is not printing the output to STDERR for some reason which I have not uncovered. I did not find the output in any of my PHP, Apache, nor Event Viewer logs.

After setting curl_setopt($c, CURLOPT_STDERR, fopen(‘/curl.txt’, ‘w+’)); , I was able to see the output in the curl.txt file.

I am not sure if this is specific to Windows environments.

See what CURL sends from a PHP script

Thanks for all the answers! After all, they tell that It’s not possible. I went down the road and got familiar with Wireshark. Not an easy task but definitely worth the effort.

Источник

How to debug curl in PHP?

The following was taken from a post on MSDN (http://blogs.msdn.com/b/azureossds/archive/2015/06/12/verify-peer-certificate-from-php-curl-for-azure-apps.aspx) Common error messages related to SSL_VERIFYPEER option could be: SSL certificate problem, verify that the CA cert is OK SSL certificate problem: unable to get local issuer certificate The error is usually caused by missing or having invalid SSL certificate in cURL option. If you see these messages, consider to validate SSL certificate, and check the path to CA certificate file.

How to debug curl in PHP?

I want to test whether everything is okay or not with the curl in my PHP installation,

I tried the below code, but it I got internal server error! Would anybody help me with this.

  // Close handle curl_close($ch); ?> 

Add the following code in the start and try again.

ini_set('display_startup_errors', 1); ini_set('display_errors', 1); error_reporting(E_ALL); 

And if there is any parsing issue, then you will have to edit php.ini file and set this param

try this use this into before curl close

Php — after curl post, how to check the postfield, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more

How to debug a get request in php using curl

I’m trying to make a get request in php using cURL. This is what I’m doing:

$curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_USERPWD, "username:password"); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($curl); printf($result); 

But $result doesn’t print out anything, no success or failure message. I’ve successfully reached the endpoint via postman and in a web browser so I know it works. Printing out $curl prints: «Resource #1» which makes me think curl is properly installed on the server.

I’m not sure what steps to take next to make things work.

Add a few more option for troubleshooting purposes.

Check for an error response.

If no error, get the details:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT,10); curl_setopt($ch, CURLOPT_FAILONERROR,true); curl_setopt($ch, CURLOPT_ENCODING,""); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLINFO_HEADER_OUT, true); curl_setopt($ch, CURLOPT_HEADER, true); $data = curl_exec($ch); if (curl_errno($ch)) < $data .= 'Retreive Base Page Error: ' . curl_error($ch); >else < $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); $head = substr($data,0,$skip); $data = substr($data,$skip); $info = curl_getinfo($ch); $info = var_export($info,true); >echo $head; echo $info; 

You can utilize curl’s curlopt_verbose and curlopt_stderr like this:

$curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_USERPWD, "username:password"); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $verbose = fopen('php://temp', 'w+'); curl_setopt($curl, CURLOPT_VERBOSE, true); curl_setopt($curl, CURLOPT_STDERR, $verbose); $result = curl_exec($curl); curl_close($curl); rewind($verbose); $verboseLog = stream_get_contents($verbose); echo "Verbose information:\n
", htmlspecialchars($verboseLog), "

\n"; printf($result);

PHP cURL custom headers, I’m wondering if/how you can add custom headers to a cURL HTTP request in PHP. I’m trying to emulate how iTunes grabs artwork and it uses these non-standard headers: X-Apple-Tz: 0 X-Apple-Store-Front: 143444,12

Steps for debugging php Curl on windows

I had a lot of trouble figuring out why my php Curl API worked fine on a Mac using MAMP, but would not work under windows.

I asked fot debugging tips or useful information for finding curl configuration issues under windows.

The accepted answer contains a list of the steps that helped me get curl working on windows 7 32 bits.

If Curl still doesn’t work, you can use file_get_contents to make POST requests. It works on all hostingers, all OS and on local.

 $url = 'WhateverUrlYouWant'; $postdata = http_build_query( array( 'id' => '202', 'form' => 'animal', . ) ); $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata ) ); $context = stream_context_create($opts); $result = file_get_contents($url,false, $context); echo $result 

Here is a list of steps for debugging Curl:

Check in phpinfo module that Curl IS enabled.

Verify extensions dir path is propperly set in php.ini and that extension=php_curl.dll is uncommented.

Check that Environment Variables are propperly set as per: http://php.net/manual/en/faq.installation.php#faq.installation.addtopath

Run deplister.exe ext\php_curl.dll to verify all dependencies are correctly satisfied.

If all of the above is working then check the output of CURLOPT_VERBOSE. As per @hp95 suggestion in the following thread: No Response getting from Curl Request to https server

If you are reaching a site that uses SSL check the following post: HTTPS and ssl3_get_server_certificate:certificate verify failed, CA is OK

  • print_r($response); curl_close($curl); ** print your results BEFORE closing, which destroys (empties) the $response **

Using XDebug to trace a PHP web service page, How to debug PHP pages accessed through PHP cURL using Netbeans and XDebug. 0. PHP Web service development using Netbeans. 0. Attaching Netbeans to XDebug on XAMPP Webservice fails. Related. 43. xdebug remote debugging won’t stop at breakpoints. 4930. Reference — What does this symbol mean in …

How to debug curl in PHP and Azure

Ok, I’ve got a Drupal site which I’m deploying on Azure. My App perform an API call to Marketo to trigger an action in Marketo’s API.

On my local dev and on a Debian server (regular lamp stack) all works fine.

When I deploy the site to Azure, it doesn’t work and all I get is NULL .

 public function postData()< $url = $this->host . "/rest/v1/campaigns/" . $this->id . "/schedule.json?access_token=" . $this->getToken(); $ch = curl_init($url); $requestBody = $this->bodyBuilder(); print_r($requestBody); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json','Content-Type: application/json')); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); curl_getinfo($ch); $response = curl_exec($ch); dvm($response, $name = NULL); return $response; > private function getToken()< $ch = curl_init($this->host . "/identity/oauth/token?grant_type=client_credentials&client_id=" . $this->clientId . "&client_secret=" . $this->clientSecret); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: application/json',)); curl_setopt($ch, CURLOPT_VERBOSE, true); $response = json_decode(curl_exec($ch)); curl_close($ch); $token = $response->access_token; dvm($response, $name = NULL); return $token; > 

Please note:

There are 2 calls: one to get the access_token and another the actual trigger.

dvm($response, $name = NULL); in simplistic terms is a Drupal equivalent to print_r

This exact same code works on my local machine (OS X, Apache, PHP5.6) and on Debian (Apache, PHP 5.6).

I’m using SQLite which is not exactly relevant but I though I should add just in case.

When I execute this code locally or on the Debian server I get the $result variable printed which tells me whether it was successful or not. On Azure I get just NULL for the first FALSE to the second.

The variables ( $this->id , $this->getToken() , etc) are correctly set and I can I print them fine. Like I said, all works locally.

Default settings on Azure has CURLOPT_SSL_VERIFYPEER set equal to TRUE. If you dont have an SSL there could be an issue.

The following was taken from a post on MSDN (http://blogs.msdn.com/b/azureossds/archive/2015/06/12/verify-peer-certificate-from-php-curl-for-azure-apps.aspx)

Common error messages related to SSL_VERIFYPEER option could be:

SSL certificate problem, verify that the CA cert is OK SSL certificate problem: unable to get local issuer certificate

The error is usually caused by missing or having invalid SSL certificate in cURL option. If you see these messages, consider to validate SSL certificate, and check the path to CA certificate file. CA certificate must be in PEM format, for more detail about CA extract, visit http://curl.haxx.se/docs/caextract.html

Do not turn off CURLOPT_SSL_VERIFYPEER unless your cURL connect to non certificate protected server.

There are two ways that you can specify certificate info for cURL in PHP environment.

  1. Specify curlopt_cainfo in cURL option: (sample code) curl_setopt($ch, CURLOPT_CAINFO, getcwd() . «\cert\ca-bundle.crt»); Note: getcwd() . «\cert\ca-bundle.crt» returns absolute path of your ca-bundle.crt. Make sure ca-bundle is installed at correct path.
  2. Set curl.cainfo in php.ini In php.ini file, find [curl] section, add this directive (sample code): curl.cainfo =»D:\home\site\wwwroot\cert\ca-bundle.crt» Note: Restart you web app after the change.
 “curl.cainfo” is system level directive which has to be set in php.ini, not in .user.ini. To use this approach, need to have customer php.ini. Refer to the link to setup customer php.ini, https://github.com/projectkudu/kudu/wiki/Xdt-transform-samples#using-a-custom-phpini 

CURLOPT_SSL_VERIFYHOST option is used along with verify peer, default value of this option is 2, to check the existence of a common name and also verify that it matches the hostname provided (more detail at http://php.net/manual/en/function.curl-setopt.php)

What Steps do you Take to Troubleshoot Problems with, php debugging http curl. Share. Improve this question. Follow edited May 23, 2017 at 12:00. Community Bot. 1 1 1 silver badge. asked May 2, 2009 at 23:39. Alan Storm Alan Storm. 161k 88 88 gold badges 382 382 silver badges 577 577 bronze badges. 1. just use CURLOPT_HEADER to get the header.. No need …

Источник

Читайте также:  Весь язык программирования php
Оцените статью